<?php
/**
 *
 * 版权所有：恰维网络<qwadmin.qiawei.com>
 * 作    者：寒川<hanchuan@qiawei.com>
 * 日    期：2016-09-20
 * 版    本：1.0.0
 * 功能说明：文章控制器。
 *
 **/

namespace Qwadmin\Controller;

use Vendor\Tree;
use Think\Model;
header("Content-type: text/html; charset=utf-8");
class NovelController extends ComController
{

	//ajax修改主编推荐值和首页轮播排序值
	public function ajaxedit(){
		$data = I('post.');
		if ($data['type']=='editor-recommend') {
			$novel_id = $data['novel_id'];
			$order_set = $data['value'];
			$data = ['order_set'=>$order_set];
			$result = M("novel")->where("id = $novel_id")->save($data);
		}
		if ($data['type']=='carousel-order') {
			$novel_id = $data['novel_id'];
			$flow_set = $data['value'];
			$data = ['flow_set'=>$flow_set];
			$result = M("novel")->where("id = $novel_id")->save($data);
		}
		if ($result!=false) {
			$redata['status'] = 1;
			$redata['content'] = 'ok';
			$this->ajaxReturn($redata);
		}
	}

    public function freenovel(){
        $rows = D('user_buy')->field('novel_id')->group('novel_id')->select();
        $r1 = D('novel')->field('id')->select();
        //dump($r1);die;
        foreach ($r1 as $v){
            foreach ($rows as $v1){
                if ($v1['novel_id'] == $r1['id']) {

                }
            }
            dump($v['id']);
        }
        die;
        dump($rows);die;
    }

	public function add()
	{


		//获得小说的基本分类
		$class_list = D("novel_class")->where("is_other = 0")->select();
		$this->assign("class_list",$class_list);
		//获得小说的其他分类
		$other_class_list = D("novel_class")->where("is_other = 1")->select();
		$this->assign("other_class_list",$other_class_list);

		$this->display('form');
	}

	public function index($sid = 0, $p = 1)
	{
		//获得小说的基本分类
		$class_list = D("novel_class")->where("is_other = 0")->select();

		$this->assign("class_list",$class_list);
		//获得小说的其他分类
		$other_class_list = D("novel_class")->where("is_other = 1")->select();

		$this->assign("other_class_list",$other_class_list);

		//查询条件
		if(!empty($_GET["name"])) {
			$map["name"] = array("like","%".$_GET["name"]."%");
			$this->assign("search_name",$_GET["name"]);
		}
		if(!empty($_GET["class_id"])){
			$map["class_id"] = $_GET["class_id"];
			$this->assign("search_class_id",$_GET["class_id"]);
		}
		if(!empty($_GET["other_class_id"])){
			$map["other_class_id"] = $_GET["other_class_id"];
			$this->assign("search_other_class_id",$_GET["other_class_id"]);
		}
		if(!empty($_GET["from"])){
			$map["from"] = $_GET["from"];
			$this->assign("from",$_GET["from"]);
		}
		if($_GET["is_end"] != -1 && $_GET["is_end"] != ""){
			$map["is_end"] = $_GET["is_end"];
			$this->assign("is_end",$_GET["is_end"]);
		}

		if($_GET["is_shelf"] != -1 && $_GET["is_shelf"] != ""){
			$map["is_shelf"] = $_GET["is_shelf"];
			$this->assign("is_shelf",$_GET["is_shelf"]);
		}

		//dump($_GET);die;
		

		$order = "order_set asc,id desc";
        if(!empty($_GET["order_set"]))
            $order= "order_set asc";
        elseif(!empty($_GET["flow_set"]))
            $order= "flow_set asc";
        elseif (!empty($_GET["pay_count"]))
            $order= "count desc";
        elseif (!empty($_GET["click_count"]))
            $order= "click_count desc";

		$p = $_GET["p"] ? $_GET["p"] : 1;
		$novel = M('Novel');
		$pagesize = 20;#每页数量
		$offset = $pagesize * ($p - 1);//计算记录偏移量
		$count = $novel->where($map)->count();
		//接收值
		//查询条件处理
		$where = "1=1";
		foreach($map as $key=>$item){
			if($key != 'name')
				$where .= " and $key = '$item'";
			else 
				$where .= " and $key like '".$item[1]."'";
		}
		if($_GET['pay_set']){
            $list = D('novel')->field('qw_novel.*,sum(pay_money)')->
            join("qw_user_buy ON qw_user_buy.novel_id=qw_novel.id")
                ->group('novel_id')->order('sum(pay_money) desc')->page($p.','.$pagesize)->select();
            $list1 = D('novel')->field('qw_novel.*,sum(pay_money)')->
            join("qw_user_buy ON qw_user_buy.novel_id=qw_novel.id")
                ->group('novel_id')->order('sum(pay_money) desc')->select();
            $count = count($list1);

        }else{
            $list = $novel->query("select a.*,(SELECT count(1) as count FROM `qw_user_buy` as b where b.novel_id = a.id) as count from qw_novel as a where $where ORDER BY $order limit $offset,$pagesize");
        }




		foreach($list as $key=>$x){
			$dir="<img src= ".$x['img']." /> ";
			$pay_rs = D()->query("select sum(pay_money) as pay_money from qw_user_buy where novel_id = ".$x["id"]);
			$list[$key]["pay_money"] = $pay_rs[0]["pay_money"];
			
		}
		//print_r($list);die;

		$page = new \Think\Page($count, $pagesize);
		$page = $page->show();
		$this->assign('list', $list);
		$this->assign('page', $page);
		$this->display();

	}

	public function desc(){
	//$data = I('post.json_data['number']');
	$arr = json_decode($_POST['number'],true);
	print_r($arr);
	if($arr==1){
		$list = $novel->where($map)->order("order_set desc,id desc")->limit($offset . ',' . $pagesize)->select();
	}else{
		$list = $novel->where($map)->order("order_set asc,id desc")->limit($offset . ',' . $pagesize)->select();
	}
	}
	public function del()
	{

		$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : false;
		if ($id) {
			if (is_array($id)) {
				$id = implode(',', $id);
				$map['id'] = array('in', $id);
			} else {
				$map = 'id=' . $id;
			}
			if (M('novel')->where($map)->delete()) {
				addlog('删除文章，AID：' . $id);
				$this->success('恭喜，文章删除成功！');
			} else {
				$this->error('参数错误！');
			}
		} else {
			$this->error('参数错误！');
		}

	}

	public function edit($id)
	{
		$id = intval($id);

		$novel = M('novel')->where('id=' . $id)->find();

		if ($novel) {
			$this->assign('novel', $novel);
			//获得小说的基本分类
			$class_list = D("novel_class")->where("is_other = 0")->select();
			$this->assign("class_list",$class_list);
			//获得小说的其他分类
			$other_class_list = D("novel_class")->where("is_other = 1")->select();
			$this->assign("other_class_list",$other_class_list);

		} else {
			$this->error('参数错误！');
		}
		$this->display('form');

	}
	public function update($novel_id= 0)
	{
		$novel_id= intval($novel_id);
		
		$data['class_id'] = isset($_POST['class_id']) ? intval($_POST['class_id']) : 0;
		$data['class_name'] = isset($_POST['class_name']) ? $_POST['class_name'] : "";
		$data['other_class_id'] = isset($_POST['other_class_id']) ? $_POST['other_class_id'] : 0;
		$data['other_class_name'] = isset($_POST['other_class_name']) ? $_POST['other_class_name'] : "";
		$data['name'] = isset($_POST['title']) ? $_POST['title'] : '';
		$data['writer'] = isset($_POST['writer']) ? $_POST['writer'] : '';
		$data['description'] = isset($_POST['description']) ? $_POST['description'] : '';
		//截取路径
		$str = C('FILE_HOST_PATH');
		$img = str_replace($str,"",I('post.thumbnail', '', 'strip_tags'));
        //推广图片
        $img1 = str_replace($str,"",I('post.thumbnail1', '', 'strip_tags'));
        //mini图片
        $img2 = str_replace($str,"",I('post.thumbnail2', '', 'strip_tags'));
        $data['send_img'] = $img1;
		$data['img'] = $img;
		$data['mini_img'] = $img2;
		$data['order_set'] = isset($_POST['order_set']) ? $_POST['order_set'] : '10000';
		$data['flow_set'] = isset($_POST['flow_set']) ? $_POST['flow_set'] : '10000';
		$data['publish_time'] = date("Y-m-d H:i:s");
		$data['is_flag'] = I('post.is_flag');
		$data['is_shelf']=I('is_shelf');
		$data['is_end']=I('is_end');
        $data['free_chapter'] = isset($_POST['free_chapter']) ? $_POST['free_chapter'] : 20;
        $data['subscribe_chapter'] = isset($_POST['subscribe_chapter']) ? $_POST['subscribe_chapter'] : 8;
		
		//print_r($data);die;
		if (!$data['name'] or !$data['class_id'] or !$data['other_class_id'] or !$data['img']) {
			$this->error('警告！小说分类、特别分类、标题及小说封面为必填项目。');
		}
		if ($novel_id) {
			M('novel')->data($data)->where('id=' . $novel_id)->save();
			addlog('编辑小说基本信息，novel_id：' . $novel_id);
			$this->success('恭喜！信息编辑成功！', "index.html");
		} else {
			$aid = M('novel')->data($data)->add();
			if ($aid) {
				addlog('新增小说，novel_id：' . $novel_id);
				$this->success('恭喜！小说新增成功！', "index.html");
			} else {
				$this->error('抱歉，未知错误！');
			}

		}
	}

	/**
	 * 文章列表
	 * */
	public function chapter($p = 1){
		if(isset($_GET["id"])){
			$id = $_GET["id"];//这个是文章id
			$novel = M('novel')->where(array('id'=>$id))->find();

			$this->assign('novel',$novel);
			$this->assign("novel_id",$id);
			$p = intval($p) > 0 ? $p : 1;
			$chapter= M('novel_chapter');
			$pagesize = 15;#每页数量
			$offset = $pagesize * ($p - 1);//计算记录偏移量
			$map["novel_id"] = $id;
			$count = $chapter->where($map)->count();
			header("Content-type:text/html;charset=utf-8");
			$list = $chapter->where($map)->order("order_set asc")->limit($offset . ',' . $pagesize)->select();
			$page = new \Think\Page($count, $pagesize);
			$page = $page->show();
			$this->assign('list', $list);
			$this->assign('page', $page);
			$this->display();
		}
		else{
			$this->error('请选择一篇小说',"index.html");
		}
	}
	/**
	 * 添加章节
	 * */
	public function addchapter(){
		$id = $_GET["novel_id"];
		$novel = D("novel")->where("id=".$id)->find();
		$this->assign("novel_id",$novel["id"]);
		$this->assign("novel_mini_img",$novel["mini_img"]);
		$this->assign("novel_name",$novel["name"]);

		$this->display('chapterform');
	}

	/**
	 * 编辑章节
	 * */
	public function editchapter($id){
		$id = intval($id);
		$chapter = M('novel_chapter')->where('id='.$id)->find();
		if ($chapter) {

			$this->assign('chapter', $chapter);
			$this->assign('novel_id', $chapter["novel_id"]);
			$this->assign('novel_name', $chapter["novel_name"]);
			$this->assign('novel_mini_img', $chapter["novel_mini_img"]);
			$this->assign('chapter_id', $chapter["id"]);
		} else {
			$this->error('参数错误！');
		}
		$this->display('chapterform');
	}

	/**
	 * 更新章节
	 * */
	public function updatechapter($chapter_id){
		$chapter_id= intval($chapter_id);
		$data['chapter'] = isset($_POST['chapter']) ? $_POST['chapter'] : '';
		$data['title'] = isset($_POST['title']) ? $_POST['title'] : '';
		$data['content'] = isset($_POST['content']) ? $_POST['content'] : '';
		$data['novel_id'] = isset($_POST['novel_id']) ? intval($_POST['novel_id']) : 0;
		$data['novel_name'] = isset($_POST['novel_name']) ? $_POST['novel_name'] : '';
		$data['novel_mini_img'] = isset($_POST['novel_mini_img']) ? $_POST['novel_mini_img'] : '';
		$data['order_set'] = isset($_POST['order_set']) ? $_POST['order_set'] : 1000;
		$data['add_time'] = date("Y-m-d H:i:s");
		if (!$data['chapter'] or !$data['title'] or !$data['content'] or !$data['novel_id']) {
			$this->error('警告！章节，标题、内容、关联小说ID必填项目。');
		}
		if ($chapter_id) {
			M('novel_chapter')->data($data)->where('id=' . $chapter_id)->save();
			addlog('编辑小说基本信息，novel_id：' . $novel_id);
			$this->success('恭喜！章节编辑成功！', "chapter/id/".$data['novel_id'].".html");
		} else {
			$aid = M('novel_chapter')->data($data)->add();
			if ($aid) {
				addlog('新增小说，novel_id：' . $novel_id);
				$this->success('恭喜！章节新增成功！', "chapter/id/".$data['novel_id'].".html");
			} else {
				$this->error('抱歉，未知错误！');
			}

		}
	}

	/**
	 * 删除章节
	 * */
	public function chapterdel(){
		$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : false;
		if ($id) {
			if (is_array($id)) {
				$id = implode(',', $id);
				$map['id'] = array('in', $id);
			} else {
				$map = 'id=' . $id;
			}
			if (M('novel_chapter')->where($map)->delete()) {
				addlog('删除文章，AID：' . $id);
				$this->success('恭喜，文章删除成功！');
			} else {
				$this->error('参数错误！');
			}
		} else {
			$this->error('参数错误！');
		}
	}

	/**
	 * 小说更新
	 * 只支持最爽小说平台
	 * */
	public function updatenovel(){
		set_time_limit(0);
		$start_id = $_GET["start_id"] ? $_GET["start_id"] : 0;
		$nmap["is_end"] = 0;
		$nmap["from"] = "最爽小说";
		$update_novel_list = D("novel")->where($nmap)->order("id asc")->limit("$start_id, 1")->select();
		//print_r($update_novel_list);die;
		//COOKIE值
		//$cookie = "UM_distinctid=15c48573c78204-0470d285b-6a320e21-1fa400-15c48573c79a98; PHPSESSID=260a7e0f704511d86f4bf77b84be168d; uid=21433576; openid=ow7-lwD5Stboe4pp2nW4DD4A3cyw; uname=1f19be8cede01df4936b3fb3249be4; checkstr=e1745060a715236a1bb7f6fae365ab52; sid=260a7e0f704511d86f4bf77b84be168d; sex=2; CNZZDATA1261580920=750510054-1495858397-https%253A%252F%252Fopen.weixin.qq.com%252F%7C1496216642; channel_id=1; promotions_id=1";
		$cookie = "PHPSESSID=4afd6ed271581bb277432075dbd13e38; uid=23014196; openid=ow7-lwOTv8-M1mAE7Q83yObdbnzk; uname=%E7%8E%8B%E6%BA%AA; checkstr=331c1a538f9ef97540576ca483ce2704; sid=4afd6ed271581bb277432075dbd13e38; sex=1; UM_distinctid=15d834a438565b-0771a1938-6a330224-1fa400-15d834a4386b11; CNZZDATA1261580920=965680347-1501146005-https%253A%252F%252Fopen.weixin.qq.com%252F%7C1501146005; channel_id=1; promotions_id=1";

		if(empty($update_novel_list)){
			addlog("更新了最爽小说");
			die("the end");
		}

		foreach($update_novel_list as $key=>$item) {
			$novel_id = $update_novel_list[0]["id"] ? $update_novel_list[0]["id"] : 3;
			//echo $novel_id;die;

			//获得小说的信息
			$novel_info = D("novel")->where("id = $novel_id")->find();

			//获得小说的最新章节
			$last_chapter = D("novel_chapter")->where("novel_id = $novel_id")->order("order_set desc")->find();
			//echo D("novel_chapter")->getLastSql();

			//echo $last_chapter["chapter"]." ".$novel_info["web_from"]."<br>";
			//模拟微信浏览器访问页面
			$header = fakeIp();
			//$cookie = "PHPSESSID=b131b3e837f61475bf37f85641e0ac6d; uid=21433576; openid=ow7-lwD5Stboe4pp2nW4DD4A3cyw; uname=%E6%AC%A7%E4%BA%9A%E4%BA%91%E4%BA%A4%E6%98%93%E5%AE%A2%E6%9C%8D-%E5%B0%8F%E4%B9%90; checkstr=e1745060a715236a1bb7f6fae365ab52; sid=b131b3e837f61475bf37f85641e0ac6d; sex=2; UM_distinctid=15c497afcb780-0df9e9464-6a320e21-1fa400-15c497afcb93a3; CNZZDATA1261580920=686923403-1495880007-https%253A%252F%252Fopen.weixin.qq.com%252F%7C1495878368; channel_id=1; promotions_id=1";
			$content = getUrlContent($novel_info["web_from"], $header, $cookie, 1);

			//echo $content;die;
			//获得小说的最新章节
			$new_start = strpos($content, '<div class="new_mulu line-b">') + strlen('<div class="new_mulu line-b">');
			$new_start = strpos($content, '<p>', $new_start) + strlen('<p>');
			$new_start = strpos($content, '<p>', $new_start) + strlen('<p>');
			$new_end = strpos($content, "</p>", $new_start);
			$new_str = substr($content, $new_start, $new_end - $new_start);
			$new_str = substr($new_str, 0, strpos($new_str, " "));
			$new_order_set = mb_substr($new_str, (mb_strpos($new_str, "第", null, "UTF-8") + mb_strlen("第", "UTF-8")), mb_strpos($new_str, "章", null, "UTF-8") - (mb_strpos($new_str, "第", null, "UTF-8") + mb_strlen("第", "UTF-8")), "UTF-8");
			//echo $new_order_set;die;


			//判断最新章节是否和我带数据相同
			if($new_order_set <= $last_chapter["order_set"]){
				echo "《".$item["name"]."》"."没有更新章节";
				continue;
			}
			else{ //不相同我们就更新他
				//获取最后一章节
				//计算它是第几页
				$order_set = $last_chapter["order_set"];
				$page = intval($order_set / 20) + 1;
				//echo $page;die;
				//计算它的总也书

				//获得第一个目录的链接地址
				$menu_start = strpos($content, '<div class="pub-btn mt-10 pb-0">') + strlen('<div class="pub-btn mt-10 pb-0">');
				$menu_start = strpos($content, '<a href="', $menu_start) + strlen('<a href="');
				$menu_end = strpos($content, '"', $menu_start);
				$menu_url = substr($content, $menu_start, $menu_end - $menu_start);
				$url_list[$page] = "https://zsxs.shu22.cn".$menu_url."&order_by=order%20asc&page=".$page;

				//获得最后一页
				$last_page = intval($new_order_set / 20) + 1;

				for($i = $page + 1; $i <= $last_page; $i++){
					$url_list[$i] = "https://zsxs.shu22.cn".$menu_url."&order_by=order%20asc&page=".$i;
				}
				//print_r($url_list);die;

				foreach($url_list as $ukey=>$uitem) {
					//echo $menu_start." ".$menu_end."<br>";
					//echo $menu_url;
					//直接打开列表页面，同时跳转到我们数据对应的那一页。
					$header = fakeIp();

					$cha_list = getUrlContent($uitem, $header, $cookie, 1);
					//echo $cha_list;
					$list_start = strpos($cha_list, '<div class="mulu-list container">') + strlen('<div class="mulu-list container">');
					$list_start = strpos($cha_list, '<ul>', $list_start) + strlen('<ul>');
					$list_end = strpos($cha_list, '</ul>', $list_start);
					$list = substr($cha_list, $list_start, $list_end - $list_start);
					//echo $list."<br>";

					$list = explode('<a href="', $list);
					//print_r($list);die;
					$index = 0;
					$data = array();
					foreach($list as $key=>$item){
						//echo $item."<br>";
						//获得URL
						$url = substr($item, 0, strpos($item, '"'));
						//获得章节
						$chapter = substr($item, strpos($item, '<span>')+strlen('<span>'),  strpos($item, '</span>') - strpos($item, '<span>')+strlen('<span>'));
						$chapter = mb_substr($chapter, 0,  (mb_strpos($chapter, "章", null, "UTF-8") + mb_strlen("章", "UTF-8")), "UTF-8");
						//获得order_set
						$item_order_set =  mb_substr($chapter, (mb_strpos($chapter, "第", null, "UTF-8") + mb_strlen("第", "UTF-8")), mb_strpos($chapter, "章", null, "UTF-8") - (mb_strpos($chapter, "第", null, "UTF-8") + mb_strlen("第", "UTF-8")), "UTF-8");
						if(strlen($url) > 0 && strlen($chapter) > 0) {
							if($item_order_set > $order_set) {
								//echo $chapter." ".$url."<br>";die;
								$header = fakeIp();
								//$cookie = "PHPSESSID=b131b3e837f61475bf37f85641e0ac6d; uid=21433576; openid=ow7-lwD5Stboe4pp2nW4DD4A3cyw; uname=%E6%AC%A7%E4%BA%9A%E4%BA%91%E4%BA%A4%E6%98%93%E5%AE%A2%E6%9C%8D-%E5%B0%8F%E4%B9%90; checkstr=e1745060a715236a1bb7f6fae365ab52; sid=b131b3e837f61475bf37f85641e0ac6d; sex=2; UM_distinctid=15c497afcb780-0df9e9464-6a320e21-1fa400-15c497afcb93a3; CNZZDATA1261580920=686923403-1495880007-https%253A%252F%252Fopen.weixin.qq.com%252F%7C1495878368; channel_id=1; promotions_id=1";
								//echo $url;die;
								//$url = "https://zsxs.shu22.cn/book/content/?bookid=2023870&chapterid=5614267";
								//$zs_url = "https://zsxs.shu22.cn/book/content/?bookid=2023870&chapterid=5571491";
								$url = substr($url, 0, strpos($url, "?"));
								$ids = substr($url, strpos($url, "content/") + strlen("content/"), strlen($url) - strpos($url, "content/") + strlen("content/"));
								$bookid = substr($ids, 0, strpos($ids, "/"));
								$chapterid = substr($ids, strpos($ids, "/") + 1, strlen($ids) - strpos($ids, "/") + 1);
								//echo $bookid." ".$chapterid;die;
								$zs_url = "https://zsxs.shu22.cn/book/content/?bookid=$bookid&chapterid=$chapterid";
								//echo $zs_url;die;
								//$bookid =
								$chapter_content = getUrlContent($zs_url,$header,$cookie,1);
								//echo $c_url."<br>";

								//判断抓取是否有效
								$is_error = strpos($chapter_content, "https://res.wx.qq.com/open/libs/weui/0.4.1/weui.css");
								if(!empty($is_error)) {
									print_r($rs);
									echo $chapter_content;
									die("我呢个去你的cookie有问题,问题出现在".$novel_info["novel_name"]."下的第".$ukey."页的第".($key)."条");
								}

								//echo $chapter_content;die;

								//判断是否已经没有钱了
								//如果没有就不添加数据
// 								$no_money = strpos($chapter_content, "<title>最爽小说-充值</title>");
// 								if(!empty($no_money))
// 									die("兄弟你没钱了")
								if(strlen($chapter_content) == 0)
									die("兄弟你没钱了");

								//echo "1111".$chapter_content;die;

								//获取标题 title
								$title_start = strpos($chapter_content, '<div class="yd-left">') + strlen('<div class="yd-left">');
								$title_start= strpos($chapter_content, '<h3>', $title_start) + strlen('<h3>');
								$title_start= strpos($chapter_content, '章', $title_start) + strlen('章');
								$title_end = strpos($chapter_content, '</h3>', $title_start);
								$title = trim(substr($chapter_content, $title_start, $title_end- $title_start));
								$title = specialSignRemove($title);
								//获取第几章节(有中文)chapeter
								$cha_start = strpos($chapter_content, '<div class="yd-left">') + strlen('<div class="yd-left">');
								$cha_start = strpos($chapter_content, '<h3>', $cha_start) + strlen('<h3>');
								$cha_start = strpos($chapter_content, '第', $cha_start);
								$cha_end = strpos($chapter_content, '章', $cha_start) + strlen('章');
								$chapter_value= substr($chapter_content, $cha_start, $cha_end- $cha_start);
								//echo $title."<br>".$chapter_value;die;

								//判断是否章节重复
								$map["novel_id"] = $novel_info["id"];
								$map["chapter"] = $chapter_value;
								$count = D("novel_chapter")->where($map)->count();
								//echo $count;die;
								if($count > 0)
									continue;

								//标题赋值
								$data[$index]["title"] = $title ? $title : " ";
								$data[$index]["chapter"] = $chapter_value;

								//获取order_set
								$order_start = strpos($data[i]["chapter"], '第') + strlen('第');
								$order_end = strpos($data[i]["chapter"], '章', $order_start);
								$data[$index]["order_set"] = substr($data[$index]["chapter"], $order_start, $order_end- $order_start);

								//获取章节内容
								$c_pos_start = strpos($chapter_content, '<div class="yd-text" style="font-size:16px;">') + strlen('<div class="yd-text" style="font-size:16px;">');
								$c_pos_end = strpos($chapter_content, '</div>', $c_pos_start);
								$data[$index]["content"]= substr($chapter_content, $c_pos_start, $c_pos_end - $c_pos_start);
								$data[$index]['word_count'] = $this->runzishu($data[$index]["content"]);

								//设置数据
								$data[$index]["novel_name"] = $novel_info["name"];
								$data[$index]["novel_id"] = $novel_info["id"];
								$data[$index]["novel_mini_img"] = $novel_info["mini_img"];
								$data[$index]["add_time"] = date("Y-m-d H:i:s");


								//print_r($data);die;

								//索引自动增长
								$index++;
							}
						}
					}
					//print_r($data);die;
					if(!empty($data))
						$number = D("novel_chapter")->addAll($data);
                    
					//推送消息
                    // if ($number > 0) {
                    //     //就证明更新了
                    //     //推送消息
                    //     $arrr = D('novel_chapter')->field('id,title,novel_id,novel_name,order_set,add_time')
                    //         ->order('id desc')->limit(1)->select();
                    //     //dump($arrr);die;
                    //     foreach ($arrr as $arr){
                    //         $push_chapter_id = $arr['id'];
                    //         $push_novel_id = $arr['novel_id'];
                    //         $push_novel_name = $arr['novel_name'];
                    //         $push_title = $arr['title'];
                    //         $push_order_set = $arr['order_set'];
                    //         $push_add_time = $arr['add_time'];
                    //         $this->novelpush($push_novel_id,$push_chapter_id,$push_novel_name,$push_order_set,$push_title,$push_add_time);
                    //     }
                    // }					
						
				}
			}
		}

// 		if($start_id > 0) {
 			//echo "the end";
 			//die;
// 		}

		$next_start_id = $start_id + 1;
		//$this->redirect("/Qwadmin/Novel/updatenovel?start_id=$next_start_id");
		$this->success("跳转到下一章节","/Qwadmin/Novel/updatenovel?start_id=$next_start_id",1);
		//echo "the end";
		//echo $content;
	}

    /**
     * 小说更新
     * 只支持全本小说
     */
   public function updatenovel3(){
        set_time_limit(0);
        //获取全本小说未完结的小说的信息
       //echo 11;die;
        $where['is_end'] = 0;
        $where['from'] = '全本小说';
        $novel_start = $_GET['novel_start'] ? $_GET['novel_start'] :0;
        $no_end_novel = D('novel')->where($where)->limit("$novel_start,1")->select();
       //
        //dump($no_end_novel);die;
		$cookie = 'ikanshuuser=userid=141760945&username=170619092244708337966005&channelid=3725&sessionid=hxkb4mcm4tdpopcvdpi41v4g&checkcode=27ad789c2e14a1286c1b28282876e5aa&version=1;
                    Hm_lpvt_d3a69bde0c513dc29f82d2f573aaceff=1497836439;
                    Hm_lvt_d3a69bde0c513dc29f82d2f573aaceff=1497835352;
                    ASP.NET_SessionId=hzppzb5m0ejj01dumpeqdvt2;
                    ikscnid=rndid=170619092308513978&channelid=3603; ' ;

        if (empty($no_end_novel)) {
        	addlog("更新了全本小说");
            die('the end');
        }
//                dump($no_end_novel);die;

        //一本的小说
        foreach ($no_end_novel as $key => $item){
            //小说id
            $novel_id = $item['id'];

            //网页链接地址
            $web_from = $item['web_from'];
//            echo $web_from;die;
//            echo $novel_id;die;s
            //根据小说去查找章节表的最后一章
            $where1['novel_id'] = $novel_id;

            //根据小说id获取最后一条章节的数据
            $last_chapter = D('novel_chapter')->where($where1)->order('id desc')->limit(1)->find();
//            dump($last_chapter);die;

            if(empty($last_chapter)){
                echo '小说为空';
                continue;
            }

            //数据库最后的章节
            $start = $last_chapter['order_set'];
//            echo $start;die;

            $add_data = array();
            $add_index = 0;
            while(1){
//                echo $web_from;die;
//                $url = $item["web_from"]."?start=".$start."&fetch_novel=0";
                $url = $web_from.'?start='.$start.'&fetch_novel=0';

                //获取抓取微信端全文小说的最后一章
                $header = fakeIp();
                //获取所有章节的内容
                $all_content = getUrlContent($url, $header, $cookie, 1);
//                echo $all_content;die;
                preg_match_all('/\/book\/\d+\/(\d+)\.html/',$all_content,$lianjia);
//                dump($lianjia);die;
                //最后一章的章节数
                $web_last_chapter = array_pop($lianjia[1]);
//                dump($web_last_chapter);die;
                if($start == $web_last_chapter){
                    echo $no_end_novel[$key]['name'].'已经是最新章节了'.'<br/>';
                }

                //开始采集的章节数
                $caiji_start = $start + 1;

                $lianjie = array_shift($lianjia[0]);
                preg_match('/\/book\/(.*?)\/(.*?)\.html/',$lianjie,$chapter_start);
                //循环采集更新的小说
                for($i = $caiji_start; $i <= $web_last_chapter; $i++){
                    $lianjie_start_url = 'http://liweih5.ikanshu.cn/book/'.$chapter_start[1].'/'.$i.'.html';
//                echo $lianjie_start_url;echo '<br/>';
                    //得到需要采集的章节
                    $caiji_start_content = getUrlContent($lianjie_start_url, $header, $cookie, 1);
                    //echo $caiji_start_content;

                    //判断余额不足
                    preg_match('|您的账户余额不足，请进入充值或选择下面的快捷充值|i',$caiji_start_content,$nomoney);
                    if(!empty($nomoney)){
                        echo '(您的账户余额不足,请及时充值)';die;
                    }

                    //真正的采集小说具体内容了
                    $tmp_arr = array();
                    $tmp_arr['chapter'] = '第'.$i.'章';

                    //获取标题
                    $title_start = strpos($caiji_start_content,'<div class="r-chaptername" id="lbChapterName">')+strlen('<div class="r-chaptername" id="lbChapterName">');
                    $title_end = strpos($caiji_start_content,'</div>',$title_start);
                    $title_count = substr($caiji_start_content,$title_start,$title_end-$title_start);
//                echo $title_count;echo '<br>';

                    if (strpos($title_count,'章')) {
                        $title_count_arr = explode('章',$title_count);
                        //dump($title_count_arr);
                        $title = array_pop($title_count_arr);
                        $title = trim($title);
                        //dump($title);
                    }else{
                        $title = $title_count;
                        //echo $title;
                    }

                    //过滤特殊符号和不标准的字符串
                    if(strpos($title,'hapter')){
                        $title = ' ';
                    }
                    $title = specialSignRemove($title);
                    $tmp_arr['title'] = isset($title) ? $title : '';

                    //获取小说内容
                    $novel_content_start = strpos($caiji_start_content,'<div class="r-content" id="uiContent">')+strlen('<div class="r-content" id="uiContent">');
                    $novel_content_end = strpos($caiji_start_content,'</div>',$novel_content_start);
                    $novel_content = substr($caiji_start_content,$novel_content_start,$novel_content_end - $novel_content_start);
                    //替换标签
                    $novel_content = '<p>'.str_replace('<br/>','</p><p>',$novel_content);

                    //将小说内容装进$data中去
                    $tmp_arr['content'] = $novel_content;
                    $tmp_arr['word_count'] = $this->runzishu($tmp_arr['content']);
                    $tmp_arr['order_set'] = $i;
                    $tmp_arr["add_time"] = date("Y-m-d H:i:s");
                    $tmp_arr['novel_name'] = $no_end_novel[$key]['name'];
                    $tmp_arr['novel_id'] = $novel_id;
                    $tmp_arr['novel_mini_img'] = $no_end_novel[$key]['img'];

                    //查询重复章节,如果有就不插入没有就插入
                    $c_chapter['order_set'] = $i;
                    $c_chapter['novel_id'] = $novel_id;
                    //如果＞0,就说明章节重复
                    $count = D('novel_chapter')->where($c_chapter)->count();
                    if($count > 0 ){
                        continue;
                    }

                    $add_data[$add_index] = $tmp_arr;
                    $add_index++;
                }
                //dump($add_data);
                // if(!empty($add_data)) {
                //     $number = D('novel_chapter')->addAll($add_data);
                //     //推送消息
                //     if ($number > 0) {
                //         //就证明更新了
                //         //推送消息
                //         $arrr = D('novel_chapter')->field('id,title,novel_id,novel_name,order_set,add_time')
                //             ->order('id desc')->limit(1)->select();
                //         foreach ($arrr as $arr){
                //             $push_chapter_id = $arr['id'];
                //             $push_novel_id = $arr['novel_id'];
                //             $push_novel_name = $arr['novel_name'];
                //             $push_title = $arr['title'];
                //             $push_order_set = $arr['order_set'];
                //             $push_add_time = $arr['add_time'];
                //             $this->novelpush($push_novel_id,$push_chapter_id,$push_novel_name,$push_order_set,$push_title,$push_add_time);
                //         }
                //     }
                // }
                //问题开始
                if( ($web_last_chapter - $caiji_start) < 50 ){
                    break;
                }else{
                    $start += 50;
                }
                //问题结束
            }
        }
		$domain = $_SERVER["HTTP_HOST"];
        $this->success('抓取下篇小说','http://'.$domain.'/Qwadmin/novel/updatenovel3?novel_start='.($novel_start + 1),1);
    }

	/**
	 * 采集小说目录
	 * */
	public function collectList(){
		$id = $_GET["id"];
		$this->assign("novel_id",$id);

		$p = intval($p) > 0 ? $p : 1;
		$chapter= M('novel_get');
		$pagesize = 20;#每页数量
		$offset = $pagesize * ($p - 1);//计算记录偏移量

		$map["novel_id"] = $id;
		$count = $chapter->where($map)->count();
		$list = $chapter->where($map)->order("id asc")->limit($offset . ',' . $pagesize)->select();

		$page = new \Think\Page($count, $pagesize);
		$page = $page->show();
		$this->assign('list', $list);
		$this->assign('page', $page);

		$this->display();
	}

	/**
	 * 新增采集小说目录
	 * */
	public function collectAdd(){

	}

	/**
	 * 采集小说名称相关数据，不包括章节
	 * 只采集小说的名称和介绍
	 * 最爽小说针对http://zsxs.shu22.cn
	 * */
	public function getNovelInfo(){
		$rs = D("novel_get")->select();
		// print_r($rs);die;

		foreach ($rs as $key=>$item){
			$data = array();
			//获得小说图片版面内容
			$header = fakeIp();
			$novel = getUrlContent($item["web_from"],$header);

			//小说名称
			$data["name"] = $item["novel_name"];

			//判断小说是否已经存在了
			$novel_map["name"] = $item["novel_name"];
			//echo '<meta charset="UTF-8">';
			$novel_count = D("novel")->where($novel_map)->count();
			//echo D()->getLastSql();die;
			if($novel_count > 0){
				D("novel_get")->where("id = ".$item["id"])->delete();
				continue;
			}

			//来自地址
			$data["web_from"] = $item["web_from"];
			$data["from"] = $item["from"];

			//分类
			$data["class_id"] = $item["class_id"];
			$data["class_name"] = $item["class_name"];

			//男生，女生
			$data["other_class_id"] = $item["other_class_id"];
			$data["other_class_name"] = $item["other_class_name"];

			//小说图片
			$img_start = strpos($novel, '<div class="xq-left">') + strlen('<div class="xq-left">');
			$img_start = strpos($novel, '<img src="', $img_start) + strlen('<img src="');
			$img_end = strpos($novel, '"', $img_start);
			$img_url = substr($novel, $img_start, $img_end - $img_start);
			//echo $img_url;
			$img = getWebImg($img_url);
			$data["img"] = $img["save_path"].$img["file_name"];
			$data["mini_img"] = $img["save_path"].$img["file_name"];

			//总字数
			$font_start = strpos($novel, '字数：') + strlen('字数：');
			$font_end = strpos($novel, '</p>',$font_start);
			$word_count = substr($novel, $font_start, $font_end- $font_start);
			$data["word_count"] = $word_count;

			//作者
			$w_start = strpos($novel, '作者：') + strlen('作者：');
			$w_end = strpos($novel, '</p>',$w_start);
			$writer= substr($novel, $w_start, $w_end- $w_start);
			$data["writer"] = $writer;

			//是否完结
			$end_start = strpos($novel, '连载中');
			if(!empty($end_start))
				$data["is_end"] = 0;
			else
				$data["is_end"] = 1;
			//echo '<meta charset="UTF-8">';

			D("novel_get")->where("id = ".$item["id"])->delete();
			D("novel")->add($data);
// 			echo '<meta charset="UTF-8">';
// 			print_r($data);
		}

		//跳转抓取文章内容
		//获取没有爬取的所有文章
		$r_count = D("novel")->where("is_over = 0")->count();
		$url = "http://".$_SERVER["HTTP_HOST"]."/Qwadmin/Novel/newGetChapterInfo?reptile_start=0&page_start=0&page_end=".$r_count."&table_name=novel_chapter_0";
		header("Location: $url");
	}

	/**
	 * 采集小说章节
	 * 最爽小说
	 * */
	function newGetChapterInfo(){
	    //括号里边的数字是执行时间，如果为零说明永久执行直到程序结束，如果为大于零的数字，则不管程序是否执行完成，到了设定的秒数，程序结束。
        set_time_limit(0);
		$reptile_start = $_GET["reptile_start"] ? $_GET["reptile_start"] : 0;
		//查找小说数据
		$rs = D("novel")->where("`name` = '谁动了我的老婆'")->limit("0,1")->order("id asc")->select();

		//dump($rs);die;
		//分页
		$page_start = $_GET["page_start"] ? $_GET["page_start"] : 0;
		//dump($page_start);die;
		$page_end = $_GET["page_end"] ? $_GET["page_end"] : 0;
		$table_name= $_GET["table_name"] ? $_GET["table_name"] : '';
		echo '<meta charset="UTF-8">';
		// print_r($rs);die;


		//判断必要条件
		if((empty($page_start) && $page_start != 0) || empty($page_end)  || empty($table_name))
			die("not limit please set page_start and page_end and table_name");

		//判断是否达到限制
		if($reptile_start >= $page_end) {
			die("the end");
		}

		//小说展示页面地址
		$url = $rs[0]["web_from"];
		//echo $url;
		//获得小说展示页面
        //伪造ip
		$header = fakeIp();
		$cookie = "PHPSESSID=b131b3e837f61475bf37f85641e0ac6d; uid=21433576; openid=ow7-lwD5Stboe4pp2nW4DD4A3cyw; uname=%E6%AC%A7%E4%BA%9A%E4%BA%91%E4%BA%A4%E6%98%93%E5%AE%A2%E6%9C%8D-%E5%B0%8F%E4%B9%90; checkstr=e1745060a715236a1bb7f6fae365ab52; sid=b131b3e837f61475bf37f85641e0ac6d; sex=2; UM_distinctid=15c497afcb780-0df9e9464-6a320e21-1fa400-15c497afcb93a3; CNZZDATA1261580920=686923403-1495880007-https%253A%252F%252Fopen.weixin.qq.com%252F%7C1495878368; channel_id=1; promotions_id=1";
		//获取网页的内容
		$content = getUrlContent($url, $header, $cookie, 1);

		//获得小说的最新章节
		//同时计算小说有多少页
		//echo $content;
		//同时计算小说有多少页
        //strpos() 函数查找字符串在另一字符串中第一次出现的位置。
		$best_start = strpos($content, '<div class="new_mulu line-b">') + strlen('<div class="new_mulu line-b">');
		$best_start = strpos($content, '第', $best_start) + strlen("第");
		$best_end = strpos($content, "章", $best_start);
		$chapter_count = substr($content, $best_start, ($best_end - $best_start));
		//echo $chapter_count;die;

		//获得小说多少页
		if(intval($chapter_count) % 20 == 0)
			$novel_page = intval($chapter_count) / 20;
		else
			$novel_page = intval(intval($chapter_count) / 20 + 1);

		//echo $novel_page;die;

		//echo $novel_page;
		//获得小说的原始网站id
		$web_novel_id = substr($url, (strpos($url, "bookid=") + strlen("bookid=")),  strlen($url) - (strpos($url, "bookid=") + strlen("bookid=")));
		//echo $web_novel_id;die;

		//循环爬取小说
		for($i = 15; $i <= $novel_page; $i++){
// 			if($i > 1)
// 				continue;
			//每次循环的地址
			$chapter_url = "http://zsxs.shu22.cn/book/chapter?bookid=".$web_novel_id."&page=$i";
			//伪造IP
			$header = fakeIp();
			//获取网页内容
			$chapter_list = getUrlContent($chapter_url,$header,$cookie,1);

			//获得小说位置
			$list_pos_begin = strpos($chapter_list, '<div class="mulu-list container">') + strlen('<div class="mulu-list container">');
			$list_pos_begin = strpos($chapter_list, '<ul>', $list_pos_begin) + strlen('<ul>');
            $list_pos_end = strpos($chapter_list, '</ul>', $list_pos_begin);

			$chapter_list = substr($chapter_list, $list_pos_begin, $list_pos_end - $list_pos_begin);
			$chapter_list = explode('<a href="', $chapter_list);
			//print_r($chapter_list);die;
			$data = array();
			$index = 0;


			foreach($chapter_list as $key=>$item){
				$c_url = substr($item, 0, strpos($item, '"'));
				//echo $c_url."<br>";
				if(strlen($c_url) > 0){
					//echo $c_url."<br>";
					$c_url = substr($item, 0, strpos($item, '"'));
					$is_token = strpos($c_url, "token");
					if(empty($is_token)){
						$c_url = "http://zsxs.shu22.cn".$item;
					}
  				//echo $c_url."<br>";die;
// 					continue;
					//伪造IP
					//获得整夜内容
					//print_r($_SERVER);die;
					//echo $c_url;die;
					$header = fakeIp();
					$cookie = "PHPSESSID=b131b3e837f61475bf37f85641e0ac6d; uid=21433576; openid=ow7-lwD5Stboe4pp2nW4DD4A3cyw; uname=%E6%AC%A7%E4%BA%9A%E4%BA%91%E4%BA%A4%E6%98%93%E5%AE%A2%E6%9C%8D-%E5%B0%8F%E4%B9%90; checkstr=e1745060a715236a1bb7f6fae365ab52; sid=b131b3e837f61475bf37f85641e0ac6d; sex=2; UM_distinctid=15c497afcb780-0df9e9464-6a320e21-1fa400-15c497afcb93a3; CNZZDATA1261580920=686923403-1495880007-https%253A%252F%252Fopen.weixin.qq.com%252F%7C1495878368; channel_id=1; promotions_id=1";
					$chapter_content = getUrlContent($c_url,$header,$cookie,1);
					//echo $c_url."<br>";
					//echo $chapter_content;die;

					//判断抓取是否有效
					$is_error = strpos($chapter_content, "https://res.wx.qq.com/open/libs/weui/0.4.1/weui.css");
					if(!empty($is_error)) {
						print_r($rs);
						echo $chapter_content;
						die("我呢个去你的cookie有问题,问题出现在".$rs[0]["novel_name"]."下的第".$i."页的第".$key."条");
					}

					//判断是否已经没有钱了
					//如果没有就不添加数据
					$no_money = strpos($chapter_content, "<title>最爽小说-充值</title>");
					if(!empty($no_money))
						die("兄弟你没钱了");

					//echo $chapter_content;die;

					//获取标题 title
					$title_start = strpos($chapter_content, '<div class="yd-left">') + strlen('<div class="yd-left">');
					$title_start= strpos($chapter_content, '<h3>', $title_start) + strlen('<h3>');
					$title_start= strpos($chapter_content, '章', $title_start) + strlen('章');
					$title_end = strpos($chapter_content, '</h3>', $title_start);
					$title = trim(substr($chapter_content, $title_start, $title_end- $title_start));

					//获取第几章节(有中文)chapeter
					$cha_start = strpos($chapter_content, '<div class="yd-left">') + strlen('<div class="yd-left">');
					$cha_start = strpos($chapter_content, '<h3>', $cha_start) + strlen('<h3>');
					$cha_start = strpos($chapter_content, '第', $cha_start);
					$cha_end = strpos($chapter_content, '章', $cha_start) + strlen('章');
					$chapter_value= substr($chapter_content, $cha_start, $cha_end- $cha_start);
					//echo $title."<br>".$chapter_value;die;

					//判断是否章节重复
					$map["novel_id"] = $rs[0]["id"];
					$map["chapter"] = $chapter_value;
					$count = D("novel_chapter")->where($map)->count();
					//echo $count;die;
					if($count > 0)
						continue;

					//标题赋值
					$data["title"] = $title ? $title : " ";
					$data["chapter"] = $chapter_value;

					//获取order_set
					$order_start = strpos($data[i]["chapter"], '第') + strlen('第');
					$order_end = strpos($data[i]["chapter"], '章', $order_start);
					$data["order_set"] = substr($data["chapter"], $order_start, $order_end- $order_start);

					//获取章节内容
					$c_pos_start = strpos($chapter_content, '<div class="yd-text" style="font-size:16px;">') + strlen('<div class="yd-text" style="font-size:16px;">');
					$c_pos_end = strpos($chapter_content, '</div>', $c_pos_start);
					$data["content"]= substr($chapter_content, $c_pos_start, $c_pos_end - $c_pos_start);
					$data['word_count'] = $this->runzishu($data['content']);

					//设置数据
					$data["novel_name"] = $rs[0]["name"];
					$data["novel_id"] = $rs[0]["id"];
					$data["novel_mini_img"] = $rs[0]["mini_img"];
					$data["add_time"] = date("Y-m-d H:i:s");

					//索引自动增长
					$index++;

//					$new_data = $data[$key];
					//D("novel_chapter")->addAll($data);die("end");
					//print_r($data);die;
					//echo $chapter_content;
				}
			}
			//print_r($data);
			//添加章节数据20条
			if(!empty($data))
				D("novel_chapter_add")->addAll($data);
		}

		//修改当前小说为已经爬取状态
// 		$map["id"]=$rs[0]["id"];
// 		$novel_data["is_over"] = 1;
// 		D("novel")->where($map)->save($novel_data);

		//跳转下一本小说
		$this->success(
				'continue next chapter',
				"/Qwadmin/Novel/newGetChapterInfo?reptile_start=".($reptile_start+ 1)."&page_start=".$page_start."&page_end=".$page_end."&table_name=".$table_name,
				1);

	}

	/**
	 * 获得同城群聊的小说列表
	 * 只获得小说的相关信息，不包括小说的章节
	 * */
	public function getnovel2(){
		set_time_limit(0);

		//分类URL

		$class_list = array(
			array("url"=>"https://c28.818tu.com/novels/api_list_by_category/11?","name"=>"都市","class_id"=>"24","id"=>11),
			array("url"=>"https://c28.818tu.com/novels/api_list_by_category/13?","name"=>"历史","class_id"=>"27","id"=>13),
			array("url"=>"https://c28.818tu.com/novels/api_list_by_category/15?","name"=>"总裁豪门","class_id"=>"28","id"=>15),
			array("url"=>"https://c28.818tu.com/novels/api_list_by_category/16?","name"=>"官场红门","class_id"=>"29","id"=>16),
			array("url"=>"https://c28.818tu.com/novels/api_list_by_category/17?","name"=>"恐怖惊悚","class_id"=>"30","id"=>17),
			array("url"=>"https://c28.818tu.com/novels/api_list_by_category/18?","name"=>"校园青春","class_id"=>"31","id"=>18),
			array("url"=>"https://c28.818tu.com/novels/api_list_by_category/20?","name"=>"玄幻仙侠","class_id"=>"32","id"=>20),
		);

		foreach($class_list as $key=>$item){
			$start = 0;
			while(1){
				$url = $item["url"]."start=$start";
				$header = fakeIp();
				$url_list = getUrlContent($url, $header, null, 1);
				$url_list = json_decode($url_list, true);
				echo "<meta charset='utf-8'>";
				//print_r($url_list);die;
				foreach($url_list as $ukey=>$uitem){
	//				$img = getWebImg("https://ommdq027l.qnssl.com/novels/1495427096821.jpg", 1);die;
	// 				echo substr($uitem["avatar"], 0, strpos($uitem["avatar"], "?"));
	//				print_r($uitem);die;

					$data["name"] = $uitem["title"];
					$data["description"] = $uitem["summary"];
					//获取图片
					$img = getWebImg($uitem["avatar"] , 1);
					$data["img"] = $img["save_path"].$img["file_name"];
					$data["mini_img"] = $img["save_path"].$img["file_name"];
					//类型
					$data["class_id"] = $item["class_id"];
					$data["class_name"] = $item["name"];
					$data["other_class_id"] = "3";
					$data["other_class_name"] = "男生";
					//来源
					$data["from"] = "同城群聊";
					$data["web_from"] = "https://c28.818tu.com/read/api_get_catalog/".$uitem["id"];
					//$data["from_url"] = "https://c28.818tu.com/read/novel/".$uitem["id];
					//print_r($data);
					//先查询小说是否存在
					$map["name"] = $uitem["title"];
					$count = D("novel")->where($map)->count();
					if($count == 0)
						D("novel")->add($data);
					//die("end");
				}

				if(count($url_list) < 20)
					break;
				else
					$start += 20;
			}
		}

		echo "the end";
	}

	/**
	 * 获取同城群聊相关的小说章节
	 * */
	public function getchapter2(){
		set_time_limit(0);
		$id = $_GET["id"] ? $_GET["id"] :59;
		$map["from"] = "同城群聊";
		$map["id"] = $id;
		//echo $id;
		$novel_list = D("novel")->where($map)->select();
		//echo D("novel")->getLastSql();die;
		//print_r($novel_list);
		echo "<meta charset='utf-8'>";
		foreach($novel_list as $key=>$item){
			$start = 0;
			while(1){
				$header = fakeIp();
				$url = $item["web_from"]."?start=$start";

				//$url = "https://c28.818tu.com/read/novel/70?from=";
				$chapter_list = getUrlContent($url,$header,null,1);
				$chapter_list= json_decode($chapter_list, true);
// 				echo $url."<br>";
// 				if(count($chapter_list) < 50)
// 					break;
// 				else
// 					$start += 50;

// 				continue;
//				print_r($chapter_list);die;

				$add_data = array();
				$add_index = 0;
				foreach($chapter_list as $ckey=>$citem){
					$data["chapter"] = "第".$citem["idx"]."章";
					//过滤同城群聊的小说标题
					$title = $citem["title"];
					//echo $title."<br>";
					//先判断有章
// 					$z_pos = mb_strpos($title,"章",null,"UTF-8") + mb_strlen("章", "UTF-8");
// 					$j_pos = mb_strpos($title,"节",null,"UTF-8") + mb_strlen("节", "UTF-8");;
					//echo $j_pos." ".$z_pos."<br>";die;
					//die;
					if(mb_strpos($title,"章",null,"UTF-8")){
						$pos_start = mb_strpos($title,"章",null,"UTF-8") + mb_strlen("章", "UTF-8");
						$title = mb_substr($title, $pos_start, (strlen($title) - $pos_start), "UTF-8");

						//过滤特别字符
						$title = str_replace(" ", "", $title);
						$title = str_replace("、", "", $title);
						$title = str_replace("·", "", $title);
						$title = str_replace(".", "", $title);
						$title = str_replace("（", "", $title);
						$title = str_replace("）", "", $title);
						$title = str_replace("：", "", $title);
					}
					elseif(mb_strpos($title,"节",null,"UTF-8")){
						$pos_start = mb_strpos($title,"节",null,"UTF-8") + mb_strlen("节", "UTF-8");
						$title = mb_substr($title, $pos_start, (strlen($title) - $pos_start), "UTF-8");
						//echo $title." ".strlen($title)." ".$pos_start."<br>";

						//过滤特别字符
						$title = str_replace(" ", "", $title);
						$title = str_replace("、", "", $title);
						$title = str_replace("·", "", $title);
						$title = str_replace(".", "", $title);
						$title = str_replace("【", "", $title);
						$title = str_replace("】", "", $title);
						$title = str_replace("：", "", $title);

					}
					else{
						//过滤数字
						for($i=0;$i<10;$i++)
							$title = str_replace("$i", "", $title);
						//过滤特别字符
						$title = str_replace(" ", "", $title);
						$title = str_replace("、", "", $title);
						$title = str_replace("·", "", $title);
						$title = str_replace(".", "", $title);
						$title = str_replace("【", "", $title);
						$title = str_replace("】", "", $title);
						$title = str_replace("（", "", $title);
						$title = str_replace("）", "", $title);
						$title = str_replace("：", "", $title);
					}
					$data["title"] = $title;
					//echo $title;die;

					$data["chapter"] = "第".$citem["idx"]."章";

					$data["order_set"] = $citem["idx"];

					$data["add_time"] = date("Y-m-d H:i:s");

					//查询重复
					$c_map["order_set"] =  $citem["idx"];
					$c_map["novel_id"] = $item["id"];
					$count = D("novel_chapter")->where($c_map)->count();
					if($count > 0) {
						continue;
					}

					$header = fakeIp();
					$c_url = "https://c28.818tu.com/read/index/".$citem["id"]."?from=";
					$cookie = "UM_distinctid=15c48826fac86e-0dd93756d-6a320e21-1fa400-15c48826fad294; __fvmp=1; __trade_type=jsapi; CNZZDATA1260438811=1003124604-1495859966-%7C1495868746; member_token=eb611820526446529e267dca399a0c5f";
					$chapter_content = getUrlContent($c_url,$header,$cookie,1);
					$c_start = strpos($chapter_content, "<section>") + strlen("<section>");
					$c_end = strpos($chapter_content, "</section>", $c_start);
					$chapter_content = substr($chapter_content, $c_start, $c_end - $c_start);

					if(empty($chapter_content) || strlen($chapter_content) <= 0){
						echo "session失效了，id在$id";
						die;
					}


					$data["content"] = $chapter_content;

					//对应小说信息
					$data["novel_name"] = $item["name"];
					$data["novel_id"] = $item["id"];
					$data["novel_mini_img"] = $item["mini_img"];

					//print_r($data);die;

					$add_data[$add_index] = $data;
					$add_index++;
					//print_r($data);
				}

				if(!empty($add_data)) {
					//print_r($add_data);die;
					D("novel_chapter")->addAll($add_data);
				}

				if(count($chapter_list) >= 50)
					$start += 50;
				else
					break;
			}
			//die;
			break;
		}

		//echo "end";
		if(intval($id) == 285) {
			echo "is end";
			die;
		}
		else {
			$next_id = $id + 1;
			$domain = $_SERVER['HTTP_HOST'];
			$this->redirect("http://".$domain."/Qwadmin/Novel/getchapter2?id=$next_id");
		}
	}

	/**
	 * 同城群聊数据同步修改
	 * */
	public function tongchengdatachange(){
		set_time_limit(0);

		$map["from"] = '同城群聊';
		$rs = $novel = D("novel")->where($map)->select();
		foreach($rs as $key=>$item){
			$new_web_from = $item["web_from"];
			$new_start = strpos($new_web_from, "api_get_catalog/") + strlen("api_get_catalog/");
			$new_web_from = "https://c28.818tu.com/api/novels/catalog/".substr($new_web_from, $new_start, strlen($new_web_from) - $new_start);

			$data["web_from"] = $new_web_from;
			D("novel")->where("id=".$item["id"])->save($data);
		}

		echo "end";
	}




	/**
	 * 小说更新
	 * 只支持同城群聊平台
	 * */
	public function updatenovel2(){
		set_time_limit(0);
		$map["from"] = '同城群聊';
		$novel_start = $_GET["novel_start"] ? $_GET["novel_start"] : 0;
		$novel_list = D("novel")->where($map)->limit("$novel_start,1")->select();
		//echo D("novel")->getLastSql();die;
		$cookie = $_GET["cookie"] ? $_GET["cookie"] : "aliyungf_tc=AQAAAOsY8x/cogcA2ztvDrum9q6nALwU; __fvmp=1; __trade_type=jsapi; member_token=7a8c382012f149c58272ed1650891d81; Hm_lvt_d5e4918c33053db244a223b60906a4d3=1505095951; Hm_lpvt_d5e4918c33053db244a223b60906a4d3=1505095956";

		//完成
		//print_r($novel_list);die;
		if(empty($novel_list)) {
			print_r($novel_list);
			addlog("更新了同城群聊");
			die("the end");
		}

		foreach($novel_list as $key=>$item){
			//dump($item);die;
			//获得这篇小说的最后一章节
			$cmap["novel_id"] = $item["id"];
			$last_chapter = D("novel_chapter")->where($cmap)->order("id desc")->find();
			//print_r($last_chapter);die;
			//echo $item["web_from"]."?start=".$last_chapter["order_set"]."&fetch_novel=0";
			$start = $last_chapter["order_set"];
			$add_data = array();
			$add_index = 0;
			while(1){
				$url = $item["web_from"]."?start=".$start."&fetch_novel=0";
				//获得最新章节的列表
				$header = fakeIp();
				$json = getUrlContent($url, $header, $cookie, 1);
				$json = json_decode($json, true);
				//echo count($json["data"]["catalog"]);die;
				foreach($json["data"]["catalog"] as $ckey=>$citem){
// 					$c_url = "https://c28.818tu.com/read/index/".$item["id"]."?from=";
// 					$header = fakeIp();
// 					$content = getUrlContent($c_url, $header, $cookie, 1);
					//dump($citem);
					$data["chapter"] = "第".$citem["idx"]."章";
					//过滤同城群聊的小说标题
					$title = $citem["title"];
					//echo $title."<br>";
					//先判断有章
					// 					$z_pos = mb_strpos($title,"章",null,"UTF-8") + mb_strlen("章", "UTF-8");
					// 					$j_pos = mb_strpos($title,"节",null,"UTF-8") + mb_strlen("节", "UTF-8");;
					//echo $j_pos." ".$z_pos."<br>";die;
					//die;
					if(mb_strpos($title,"章",null,"UTF-8")){
						$pos_start = mb_strpos($title,"章",null,"UTF-8") + mb_strlen("章", "UTF-8");
						$title = mb_substr($title, $pos_start, (strlen($title) - $pos_start), "UTF-8");

						//过滤特别字符
						$title = str_replace(" ", "", $title);
						$title = str_replace("、", "", $title);
						$title = str_replace("·", "", $title);
						$title = str_replace(".", "", $title);
						$title = str_replace("（", "", $title);
						$title = str_replace("）", "", $title);
						$title = str_replace("：", "", $title);
					}
					elseif(mb_strpos($title,"节",null,"UTF-8")){
						$pos_start = mb_strpos($title,"节",null,"UTF-8") + mb_strlen("节", "UTF-8");
						$title = mb_substr($title, $pos_start, (strlen($title) - $pos_start), "UTF-8");
						//echo $title." ".strlen($title)." ".$pos_start."<br>";

						//过滤特别字符
						$title = str_replace(" ", "", $title);
						$title = str_replace("、", "", $title);
						$title = str_replace("·", "", $title);
						$title = str_replace(".", "", $title);
						$title = str_replace("【", "", $title);
						$title = str_replace("】", "", $title);
						$title = str_replace("：", "", $title);

					}
					else{
						//过滤数字
						for($i=0;$i<10;$i++)
							$title = str_replace("$i", "", $title);
							//过滤特别字符
							$title = str_replace(" ", "", $title);
							$title = str_replace("、", "", $title);
							$title = str_replace("·", "", $title);
							$title = str_replace(".", "", $title);
							$title = str_replace("【", "", $title);
							$title = str_replace("】", "", $title);
							$title = str_replace("（", "", $title);
							$title = str_replace("）", "", $title);
							$title = str_replace("：", "", $title);
					}
					$data["title"] = $title;
				

					$data["chapter"] = "第".$citem["idx"]."章";

					$data["order_set"] = $citem["idx"];

					$data["add_time"] = date("Y-m-d H:i:s");
					
					//查询重复
					$c_map["order_set"] =  $citem["idx"];
					$c_map["novel_id"] = $item["id"];
					$count = D("novel_chapter")->where($c_map)->count();
					if($count > 0) {
						continue;
					}
					//dump($data);
					$header = fakeIp();
					$c_url = "https://c28.818tu.com/read/index/".$citem["id"]."?from=";
					//dump($c_url);
					//$cookie = "UM_distinctid=15c48826fac86e-0dd93756d-6a320e21-1fa400-15c48826fad294; __fvmp=1; __trade_type=jsapi; CNZZDATA1260438811=1003124604-1495859966-%7C1495868746; member_token=eb611820526446529e267dca399a0c5f";
					$chapter_content = getUrlContent($c_url,$header,$cookie,1);
					//dump($chapter_content);die;
					$c_start = strpos($chapter_content, "<section>") + strlen("<section>");
					$c_end = strpos($chapter_content, "</section>", $c_start);
					$chapter_content = substr($chapter_content, $c_start, $c_end - $c_start);
					//dump($chapter_content);die;
					if(empty($chapter_content) || strlen($chapter_content) <= 0){
						echo "session失效了，id在$id";
						die;
					}


					$data["content"] = $chapter_content;
					$data['word_count'] = $this->runzishu($data['content']);

					//对应小说信息
					$data["novel_name"] = $item["name"];
					$data["novel_id"] = $item["id"];
					$data["novel_mini_img"] = $item["mini_img"];

					//print_r($data);die;

					$add_data[$add_index] = $data;
					$add_index++;
				}

				//print_r($add_data);die;

				 if(!empty($add_data)) {
				 	$number = D("novel_chapter")->addAll($add_data);
					
    //                 if ($number > 0) {
    //                     //就证明更新了
    //                     //推送消息
    //                     $arrr = D('novel_chapter')->field('id,title,novel_id,novel_name,order_set,add_time')
    //                         ->order('id desc')->limit(1)->select();
    //                     foreach ($arrr as $arr){
    //                         $push_chapter_id = $arr['id'];
    //                         $push_novel_id = $arr['novel_id'];
    //                         $push_novel_name = $arr['novel_name'];
    //                         $push_title = $arr['title'];
    //                         $push_order_set = $arr['order_set'];
    //                         $push_add_time = $arr['add_time'];
    //                         $this->novelpush($push_novel_id,$push_chapter_id,$push_novel_name,$push_order_set,$push_title,$push_add_time);
    //                     }
    //                 }					
				 }

				if(count($json["data"]["catalog"]) < 50)
					break;
				else
					$start += 50;
			}
			//die;
		}
		$domain = $_SERVER["HTTP_HOST"];

		//跳转
		$this->success("抓取下一篇小说","http://".$domain."/Qwadmin/Novel/updatenovel2?novel_start=".($novel_start + 1)."&cookie=".$cookie, 1);

	}




	public function getquanwen(){
	    //查询出分类的信息
        $class_model = M("NovelClass");
        $class_list = $class_model->where('is_other=0')->select();
        $other_list = $class_model->where('is_other=1')->select();
//        dump($other_list);die;
        $this->assign('class_list',$class_list);
        $this->assign('other_list',$other_list);

	    $this->display();
    }
    public function getquanwen1(){
//设置超时
        set_time_limit(0);
//        dump($_POST);die;

	    $keywords = $_POST['kw'];
	    //接收post传过来的值
	    $class_id = $_POST['class_id'];
	    $other_class_id = $_POST['other_class_id'];
        if (empty($class_id) || empty($other_class_id)) {
            $this->error('请选择小说分类');
        }
	    $class_model = M("NovelClass");
	    $class_name_arr = $class_model->where("class_id=$class_id")->select();
	    //小说分类名字
	    $class_name = $class_name_arr[0]['class_name'];
	    //其他分类的名字
        $other_class_arr = $class_model->where("class_id=$other_class_id")->select();
        $other_class_name = $other_class_arr[0]['class_name'];
        //echo $other_class_name;die;

	    //dump($keywords);
	    //模拟发送一个get请求到最爽小说中去
        addlog("抓取了全本小说");
        $url = 'http://liweih5.ikanshu.cn/searchbook.aspx?key='.$keywords.'&type=1';
        //echo $url;
        $header = fakeIp();
        //王溪的cookie
        $cookie = "ikanshuuser=userid=141760945&username=170619092244708337966005&channelid=3725&sessionid=hxkb4mcm4tdpopcvdpi41v4g&checkcode=27ad789c2e14a1286c1b28282876e5aa&version=1;
                    Hm_lpvt_d3a69bde0c513dc29f82d2f573aaceff=1497836439;
                    Hm_lvt_d3a69bde0c513dc29f82d2f573aaceff=1497835352;
                    ASP.NET_SessionId=hzppzb5m0ejj01dumpeqdvt2;
                    ikscnid=rndid=170619092308513978&channelid=3603; ";
        //刘熙的cookie;

        //获取网页的内容
        $content = getUrlContent($url, $header, $cookie, 1);
//        echo $content;die;

        //得到书的id
        $start = strpos($content,'<a href="/book/')+strlen('<a href="/book/');
        // a标签结束的位置和<div>开始的位置
        $end = strpos($content,'.html" class="zw_box');
        $book_id = substr($content,$start,$end-$start);

        //preg_match('|<a href="\/bookcatalog\/(.*?).html">|i',$content,$book_id);

        //抓取书的简介
        $book_url = 'http://liweih5.ikanshu.cn/book/'.$book_id.'.html';
        //将web_from的链接地址保存到数据库
        $web_from = 'http://liweih5.ikanshu.cn/bookcatalog/'.$book_id.'.html';

        $book_content = getUrlContent($book_url, $header, $cookie, 1);//成功搜索到了小说对应名
        //书的简介
//        echo $book_content;die;

        //获取小说基本信息
        //获取书的名字开始的位置
        $name_start = strpos($book_content,'<h3>')+strlen('<h3>');
        $name_end = strpos($book_content,'</h3>');
        //获取小说名字
        $novel_name = substr($book_content,$name_start,$name_end-$name_start);

        //echo $keywords;echo '<br>';echo $novel_name;die;

        //判断是否是和搜索的小说一样
        if($keywords != $novel_name){
            $this->error('您搜索的小说不存在');
        }

        //获取作者
        preg_match('|<a href="\/authorinfo.aspx(.*?)>(.*?)<\/a>|i',$book_content,$novel_author);
        //echo $novel_author[2];

        //获取字数
        //字数开始的位置
        $zishu_start = strpos($book_content,'<p>字数')+strlen('<p>字数：');
        $zishu_end = strpos($book_content,'&nbsp;万字');
        //字数
        $novel_zishu = substr($book_content,$zishu_start,$zishu_end-$zishu_start);
        //echo $novel_zishu.'万字';

        //是否完结
        $end_start = strpos($book_content,'<p>状态')+strlen('<p>状态：');
//        echo $end_start;die;
        $is_end = substr($book_content,$end_start,6);
        if($is_end == '完本'){
            $is_end = 1;
        }else{
            $is_end = 0;
        }

        //获取图片信息
        preg_match('|<img src="(.*?)\.jpg|i',$book_content,$novel_img);
        $img_url = $novel_img[1].'.jpg';
        $img_urls = [];
		$img_urls['url'] = $img_url;
		// 文件服务器接口地址
		$host = C('FILE_HOST_PATH');
		$curl_url = "$host/Home/File/uploadImg";
		// 请求远程地址 得到图片路径
		$path = curl_request($curl_url,$img_urls);
		$path = json_decode($path,true);
        $novel_base["img"] = $path["path"];
        $novel_base["mini_img"] = $path["path"];


        //描述信息
        //dump( $book_content);
        //preg_match('|<div class="subject-intro" id="uiMoreIntro">(.*?)<\/div>|i',$book_content,$novel_description);
        $novel_description_start = strpos($book_content,'<div class="subject-intro" id="uiMoreIntro">') + strlen('<div class="subject-intro" id="uiMoreIntro">');

        $novel_description_end = strpos($book_content,'</div>',$novel_description_start);
        //echo $novel_description_start; echo $novel_description_end;
        $novel_description = substr($book_content,$novel_description_start,$novel_description_end-$novel_description_start);


        //构造数据添加进数据库
        $novel_base['name'] = $keywords;
        $novel_base['description'] = isset($novel_description) ? $novel_description:' ';
        $novel_base['class_id'] = $class_id;
        $novel_base['class_name'] = $class_name;
        $novel_base['other_class_name'] = $other_class_name;
        $novel_base['other_class_id'] = $other_class_id;
        $novel_base['publish_time'] = date("Y-m-d H:i:s");
        $novel_base['word_count'] = $novel_zishu.'万字';
        $novel_base['is_end'] = $is_end;
        $novel_base['writer'] = $novel_author[2];
        $novel_base['from'] = '全本小说';
        $novel_base['web_from'] = $web_from;
        $novel_base['is_shelf'] = 0;

        //实行添加小说基本信息
        $novel_model = M('Novel');

        //判断小说名是否存在
        if ($novel_model->getByName($keywords) != null ) {
            $this->error('您搜索的小说已经存在');
        }

        //获取小说id 并同时插入数据库
        $novel_id = $novel_model->data($novel_base)->add();
        //得到所有目录
        preg_match('|<a href="\/bookcatalog\/(.*?).html">|i',$book_content,$catelog);
        $catalog_url = 'http://liweih5.ikanshu.cn/bookcatalog/'.$catelog[1].'.html';
        $catelog_content = getUrlContent($catalog_url, $header, $cookie, 1);

        //获取章节的链接地址
        preg_match_all('/\/book\/\d+\/\d+\.html/',$catelog_content,$lianjia);

        $data = array();
        $index = 0;

        //目录列表
        //dump($lianjia[0]) ;
        foreach($lianjia[0] as $key=>$item){
            //声明一个临时数组
            $tmp_arr = array();
            if($index >= 50){
                //dump($data);
                D('novel_chapter_add')->addAll($data);
                //清空数据
                $data = array();
                $index = 1;
            }

            //小说具体内容的链接
            preg_match('|\/(.*?)\/(.*?)\/(.*?)\.|i',$item,$zhangjieshu);

            //拼接链接地址
            $novel_address = 'http://liweih5.ikanshu.cn'.$item;
//            echo $novel_address;echo '<br/>';
//            continue;

            //抓取小说内容
            $chapter_content = getUrlContent($novel_address,$header,$cookie,1);
//            echo $chapter_content;die;

            //判断余额不足
            preg_match('|您的账户余额不足，请进入充值或选择下面的快捷充值|i',$chapter_content,$nomoney);
            //dump($nomoney);
            if(!empty($nomoney)) {
                //删除抓取的小说
                //echo '111';
                $novel_model->where('id='.$novel_id)->delete();
                D('novel_chapter_add')->where('novel_id='.$novel_id)->delete();
                //die('就是这里1444');
                $this->error('您的余额不足了,请及时充值');
            }

//            //判断是否章节重复
////            $map["novel_id"] = $novel_id;
////            $map["chapter"] = isset($zhangjieshu[3]) ? '第'.$zhangjieshu[3].'章' : '';
////            $count = D("novel_chapter")->where($map)->count();
////            //echo $count;die;
////            if($count > 0)
//                //continue;
//

            //获取标题
            $title_start = strpos($chapter_content,'<div class="r-chaptername" id="lbChapterName">')+strlen('<div class="r-chaptername" id="lbChapterName">');
            $title_end = strpos($chapter_content,'</div>',$title_start);
            $title_count = substr($chapter_content,$title_start,$title_end-$title_start);
//                echo $title_count;echo '<br>';
            if (strpos($title_count,'章')) {
                $title_count_arr = explode('章',$title_count);
                $title = array_pop($title_count_arr);
                $title = trim($title);
            }else{
                $title = $title_count;
                //echo $title;
            }

            //过滤特殊符号和不标准的字符串
            if(strpos($title,'hapter')){
                $title = ' ';
            }
			$title = specialSignRemove($title);
            $tmp_arr['title'] = isset($title) ? $title : '';
            $tmp_arr['chapter'] = isset($zhangjieshu[3]) ? '第'.$zhangjieshu[3].'章' : '';

            //获取小说内容
            $novel_content_start = strpos($chapter_content,'<div class="r-content" id="uiContent">')+strlen('<div class="r-content" id="uiContent">');
            $novel_content_end = strpos($chapter_content,'</div>',$novel_content_start);
            $novel_content = substr($chapter_content,$novel_content_start,$novel_content_end - $novel_content_start);
            //替换标签
            $novel_content = '<p>'.str_replace('<br/>','</p><p>',$novel_content);

            //将小说内容装进$data中去
            $tmp_arr['content'] = $novel_content;
            $tmp_arr['word_count'] = $this->runzishu($novel_content);
            $tmp_arr['order_set'] = $zhangjieshu[3];
            $tmp_arr["add_time"] = date("Y-m-d H:i:s");
            $tmp_arr['novel_name'] = $keywords;
            $tmp_arr['novel_id'] = $novel_id;
            $tmp_arr['novel_mini_img'] = $novel_base["img"];

            //将临时数组赋值给$data
            $data[] = $tmp_arr;

//            //自动增加索引
            $index++;
        }
        //循环外面
        if(!empty($data)){
            D("novel_chapter_add")->addAll($data);
        }
        //将从表(novel_chapter_add)数据同步到主表(novel_chapter)中去
        //将从表的数据查找出来
        //将所有数据插入主表中
        $sql = "insert into `qw_novel_chapter`(`chapter`,`title`,`content`,`novel_name`,`novel_id`,`novel_mini_img`,`order_set`,`add_time`,`word_count`) select `chapter`,`title`,`content`,`novel_name`,`novel_id`,`novel_mini_img`,`order_set`,`add_time`,`word_count` from qw_novel_chapter_add where  `novel_id`='$novel_id';";
        D()->execute($sql);
        //删除从表的数据
        D('novel_chapter_add')->where('novel_id='.$novel_id)->delete();
       // echo $res2;die;
        $this->success('恭喜您,已经抓取完毕');
    }

	/**
	 * 最爽小说拉取
	 * 抓取小说信息
	 */
	public function gettonovel()
	{
		if ($_POST) {
			set_time_limit(0);
			//搜索的关键字 并去除空格
			$keywords = trimall($_POST['kw']);
			//得到的基础分类id
			$cate = $_POST['cate'];
			//其他分类id
			$other_cate = $_POST['other_cate'];
			if(empty($cate)||empty($other_cate)||empty($keywords)) {
				$this->error('请输入关键字或者选择分类','gettonovel',2);
			}
			$Cate = D("novel_class")->where('class_id = '.$cate)->select();
			$OtherCate = D("novel_class")->where('class_id = '.$other_cate)->select();
			$cate_name = $Cate[0]['class_name'];//基础分类名字
			$other_class_name = $OtherCate[0]['class_name'];//其他分类名字
			//-------开始抓取小说信息---------------------------------------------------------------------------------------------
			addlog("抓取了最爽小说");
			$header = fakeIp();
            $cookie = "PHPSESSID=4afd6ed271581bb277432075dbd13e38; uid=23014196; openid=ow7-lwOTv8-M1mAE7Q83yObdbnzk; uname=%E7%8E%8B%E6%BA%AA; checkstr=331c1a538f9ef97540576ca483ce2704; sid=4afd6ed271581bb277432075dbd13e38; sex=1; UM_distinctid=15d834a438565b-0771a1938-6a330224-1fa400-15d834a4386b11; CNZZDATA1261580920=965680347-1501146005-https%253A%252F%252Fopen.weixin.qq.com%252F%7C1501146005; channel_id=1; promotions_id=1";

            //$cookie = "PHPSESSID=b131b3e837f61475bf37f85641e0ac6d; uid=21433576; openid=ow7-lwD5Stboe4pp2nW4DD4A3cyw; uname=%E6%AC%A7%E4%BA%9A%E4%BA%91%E4%BA%A4%E6%98%93%E5%AE%A2%E6%9C%8D-%E5%B0%8F%E4%B9%90; checkstr=e1745060a715236a1bb7f6fae365ab52; sid=b131b3e837f61475bf37f85641e0ac6d; sex=2; UM_distinctid=15c497afcb780-0df9e9464-6a320e21-1fa400-15c497afcb93a3; CNZZDATA1261580920=686923403-1495880007-https%253A%252F%252Fopen.weixin.qq.com%252F%7C1495878368; channel_id=1; promotions_id=1";

			$url = "http://zsxs.shu22.cn/book/searchresult?kw=".$keywords;
			$content = getUrlContent($url,$header,$cookie,1);
			//bookid第一次出现的位置
	        $start = strpos($content,'<a href="/book/?bookid=')+strlen('<a href="/book/?bookid=');
	        // a标签结束的位置和<div>开始的位置
	        $end = strpos($content,'<div class="book-two container">');
	        $len = strlen('">');
	        $length = $end - $start - $len;
	        $str_url = substr($content,$start,$length);
	        //得到bookid
	       	$book_id = subStrNumber($str_url);
	        $book_url = 'http://zsxs.shu22.cn/book/?bookid='.$book_id;//小说的来源
	        $book_content = getUrlContent($book_url, $header, $cookie, 1);//成功搜索到了小说对应名字
	        //获取书的名字开始的位置 获取小说名字
	        $name_start = strpos($book_content,'<h3>') + strlen('<h3>');
	        $name_end = strpos($book_content,'</h3>');//结束位置
	        $novel_name = substr($book_content,$name_start,$name_end - $name_start);
	        //获取作者
	        $author_start = strpos($book_content,'<p>作者') + strlen('<p>作者：');
	        $author_end = strpos($book_content,'</p>');
	        $novel_author = substr($book_content,$author_start,$author_end - $author_start);
	        // 获取字数
	        $num_start = strpos($book_content,'<p>字数：') + strlen('<p>字数：');
	        $num_end = strpos($book_content,'</p>',$num_start);
	        $num = substr($book_content,$num_start,$num_end - $num_start);
	        // echo $num;die;
	        // 获取小说状态
	        $ns_start = strpos($book_content,'章(') + strlen('章(');
	        $ns_end = strpos($book_content,')</p>');
	        $ns = substr($book_content,$ns_start,$ns_end - $ns_start);
	        //小说图片
			$img_start = strpos($book_content, '<div class="xq-left">') + strlen('<div class="xq-left">');
			$img_start = strpos($book_content, '<img src="', $img_start) + strlen('<img src="');
			$img_end = strpos($book_content, '" alt', $img_start);
			$img_url = substr($book_content, $img_start, $img_end - $img_start);
			//将网上的图片保存到本地 得到本地路径
	        //将图片路径装入数组
			$img_urls = [];
			$img_urls['url'] = $img_url;
			// 文件服务器接口地址
			$host = C('FILE_HOST_PATH');
			$curl_url = "$host/Home/File/uploadImg";
			// 请求远程地址 得到图片路径
			$path = curl_request($curl_url,$img_urls);
			$path = json_decode($path,true);
			$imgs = $path["path"];
			$mini_imgs = $path["path"];
			// 获取小说简介
			$des_start = strpos($book_content,'<span class="" id="jianjie-txt">') + strlen('<span class="" id="jianjie-txt">');
			$des_end = strpos($book_content, '</span>',$des_start);
			$description = substr($book_content, $des_start, $des_end - $des_start);
			// 判断小说是否存在
			$novel_name = trimall($novel_name);
			//var_dump($novel_name);
			if (is_numeric(strpos($novel_name,$keywords)) == false && $novel_name != $keywords) {
				$this->error('查找的小说不存在，请重新输入','gettonovel',2);
			}
			// 将数据放入数组
			$novel = M("Novel");
			$data = [];
			$data["name"] = $novel_name;
			$data["description"] = $description;
			$data["writer"] = $novel_author;
			$data["img"] = $imgs;
			$data["mini_img"] = $mini_imgs;
			$data["class_id"] = $cate;
			$data["class_name"] = $cate_name;
			$data["other_class_id"] = $other_cate;
			$data["other_class_name"] = $other_class_name;
			$data["word_count"] = $num;
			$data["is_end"] = $ns == "完结"? 1:0;
			$data["from"] = "最爽小说";
			$data["web_from"]= $book_url;
			$data["is_over"] = 1;
			$data['is_shelf'] = 0;
			//die('1627');
			
			$result = $novel->getByName($novel_name);
			if ($result !== null) {
				$this->error('该小说已经存在','gettonovel',2);
			}else{
				// $rs = M("NovelTwo")->add($data);// 这里是测试表
				$rs = $novel->add($data);
				$id = $rs;// 获取刚插入的数据的主键id
				if (!is_numeric($rs)) {
					$this->error('小说数据写入失败','gettonovel',2);
				}
			}
			//-----------------抓取小说基本信息结束-------
			//-----------------抓取章节开始---------------
			$list_url = 'https://zsxs.shu22.cn/book/chapter?bookid='.$book_id;
       	 	// 获取小说目录页面内容
        	$list_content = getUrlContent($list_url, $header, $cookie,1);
        	// echo $list_content;
        	// 得到章节数
	        $chapter_start = strpos($list_content,'<h2>目录(') + strlen('<h2>目录(');
		    $chapter_end = strpos($list_content,'章)</h2>',$chapter_start);
		    $chapter_num = substr($list_content,$chapter_start,$chapter_end - $chapter_start);
        	//获得小说章节多少页
			if(intval($chapter_count) % 20 == 0)
				$novel_page = intval($chapter_num) / 20;
			else
				$novel_page = intval(intval($chapter_num) / 20 + 1);
			// 循环爬小说章节
			for ($i = 1; $i <= $novel_page; $i++) {
				// 每次循环的小说页数地址
				$chapter_url = "http://zsxs.shu22.cn/book/chapter?bookid=".$book_id."&page=$i";
				// 得到当页内容（章节列表，不是小说章节内容）
				$chapter_list = getUrlContent($chapter_url,$header,$cookie,1);
				// var_dump($chapter_list);die;
				//获得小说位置
				$list_pos_begin = strpos($chapter_list, '<div class="mulu-list container">') + strlen('<div class="mulu-list container">');
				$list_pos_begin = strpos($chapter_list, '<ul>', $list_pos_begin) + strlen('<ul>');
				$list_pos_end = strpos($chapter_list, '</ul>', $list_pos_begin);
				$chapter_list = substr($chapter_list, $list_pos_begin, $list_pos_end - $list_pos_begin);
				$chapter_list = explode('<a href="', $chapter_list);
				$dataC = array();
				$index = 0;
				foreach ($chapter_list as $key => $item) {
					// 截取章节url
					$c_url = substr($item, 0 ,strpos($item, '"'));
					if (strlen($c_url) > 0) {
						    $c_url = substr($item, 0, strpos($item, '"'));
						    $is_token = strpos($c_url, "website_token");
						if(empty($is_token)){
								$c_url = "http://zsxs.shu22.cn".$c_url;
					 		}else{
						 		$begin = strpos($c_url, '/'.$book_id.'/') + strlen('/'.$book_id.'/');
						 		$end = strpos($c_url, '?source=', $begin);
						 		$chapter_id = substr($c_url,$begin,$end - $begin);
						 		$c_url = "http://zsxs.shu22.cn/book/content/?bookid=".$book_id."&chapterid=".$chapter_id;
					 		}
						 // 获取每一章节的内容
						$chapter_content = getUrlContent($c_url,$header,$cookie,1);
						//判断抓取是否有效
						if (empty($chapter_content)) {
							$this->novelsync($id);
							die('没钱了，请充值');
						}
						$is_error = strpos($chapter_content, "https://res.wx.qq.com/open/libs/weui/0.4.1/weui.css");
						if(!empty($is_error)) {
							// echo $novel_name;
							// echo $chapter_content;
							$this->error("你的cookie有问题,问题出现在".$novel_name."下的第".$i."页的第".$key."条",'gettonovel',3);
						}
						//判断是否已经没有钱了
						//如果没有就不添加数据
						$no_money = strpos($chapter_content, "<title>最爽小说-充值</title>");
						if(!empty($no_money))
							die("账户余额不足，请充值！");
						//获取标题 title
						$title_start = strpos($chapter_content, '<div class="yd-left">') + strlen('<div class="yd-left">');
						$title_start = strpos($chapter_content, '<h3>', $title_start) + strlen('<h3>');
						$title_start = strpos($chapter_content, '章', $title_start) + strlen('章');
						$title_end = strpos($chapter_content, '</h3>', $title_start);
						$title = trim(substr($chapter_content, $title_start, $title_end- $title_start));
						$title = specialSignRemove($title);
						//获取第几章节(有中文)chapeter
						$cha_start = strpos($chapter_content, '<div class="yd-left">') + strlen('<div class="yd-left">');
						$cha_start = strpos($chapter_content, '<h3>', $cha_start) + strlen('<h3>');
						$cha_start = strpos($chapter_content, '第', $cha_start);
						$cha_end = strpos($chapter_content, '章', $cha_start) + strlen('章');
						$chapter_value = substr($chapter_content, $cha_start, $cha_end- $cha_start);
						//判断是否章节重复
						$map["novel_id"] = $id;
						$map["chapter"] = $chapter_value;
						$count = D("novel_chapter")->where($map)->count();
						if($count > 0)
							continue;
						//标题赋值
						$dataC[$index]["title"] = $title ? $title : " ";
						$dataC[$index]["chapter"] = $chapter_value;
						//获取order_set
						$order_start = strpos($dataC[i]["chapter"], '第') + strlen('第');
						$order_end = strpos($dataC[i]["chapter"], '章', $order_start);
						$dataC[$index]["order_set"] = substr($dataC[$index]["chapter"], $order_start, $order_end- $order_start);
						//获取章节内容
						$c_pos_start = strpos($chapter_content, '<div class="yd-text" style="font-size:16px;">') + strlen('<div class="yd-text" style="font-size:16px;">');
						$c_pos_end = strpos($chapter_content, '</div>', $c_pos_start);
						$content = substr($chapter_content, $c_pos_start, $c_pos_end - $c_pos_start);
						$dataC[$index]["content"] = empty($content)?"":$content;
						//设置数据
						$dataC[$index]["novel_name"] = $novel_name;
						$dataC[$index]["novel_id"] = $id;
						$dataC[$index]["novel_mini_img"] = $mini_imgs;
						$dataC[$index]["add_time"] = date("Y-m-d H:i:s");
						//索引自动增长
						$index++;
					}
				}
				if(!empty($dataC)){
					D("novel_chapter_add")->addAll($dataC);
				}
			}
			$this->novelsync($id);
		$this->success('抓取结束','index',2);exit;
      	// ---------------------------抓取章节结束--------------------------------------------------
		}
		$cate = D("novel_class")->where("is_other = 0")->select();//基础分类
		$other_cate = D("novel_class")->where("is_other = 1")->select();//特殊分类
		$this->assign('cate',$cate);
		$this->assign('other_cate',$other_cate);
		$this->display();
	}

	/**
	 * 今天更新的小说
	 * */
	public function todayupdatenovelinfo($p = 1){
		set_time_limit(0);
		$today = date("Y-m-d")." 00:00:00";
		$pagesize = 5;#每页数量
		$offset = $pagesize * ($p - 1);//计算记录偏移量
		$data = D("novel_chapter")
		->where("add_time >= '{$today}'")
		->group("novel_id")
		->select();
		$count = count($data);
		//dump($count);die;
		$info = D("novel_chapter")
		->where("add_time >= '{$today}'")
		->group("novel_id")
		->limit($offset . ',' . $pagesize)
		->select();
		$page = new \Think\Page($count, $pagesize);
		$page = $page->show();
		$this->assign('list',$info);
		$this->assign('page', $page);
		$this->display();
	}
	/**
	 * 同步小说章节
	 * $novel_name string 小说名
	 */
	public function novelsync($id)
	{
		// 获取小说名称
		$map['novel_id'] = $id;
		// $map['novel_name'] = $novel_name;
		$novel_chapter_add = D('novel_chapter_add');
		// 得到该小说的所有章节
		$data = $novel_chapter_add
		->where($map)
		->field('chapter,title,content,novel_name,novel_id,novel_mini_img,order_set,add_time')
		->select();
		$novel_chapter = D('novel_chapter');
		// 数据插入到主表
		$rs = $novel_chapter->addAll($data);
		if (!isset($rs)) {
			$this->error('小说同步失败');
		}
		// 修改该小说的上下架状态
		$novel = M('novel');
		$map['from'] = "最爽小说";
		$map['id'] = $id;
		$re = $novel->where($map)->find();
		if ($re != null || $re != false) {
			$novel->is_shelf = 1;
			$result = $novel->where('id='.$id)->save();
		}
		// 删除之前数据表中的小说章节
		$rsl = $novel_chapter_add->where('novel_id='.$id)->delete();
		if ($rsl <1) {
			$this->error('原章节未删除');
		}
	}

	/**
	 * [gettcnovel 智能采集同城群聊小说]
	 * @return [type] [description]
	 */
	public function gettcnovel()
	{
		if (!$_POST) {
			$cate = D("novel_class")->where("is_other = 0")->select();//基础分类
			$other_cate = D("novel_class")->where("is_other = 1")->select();//特殊分类
			$this->assign('cate',$cate);
			$this->assign('other_cate',$other_cate);
			$this->display();
			die;
		}else{
			set_time_limit(0);
			//得到关键词
			$keywords = $_POST['keywords'];
			//dump($keywords);die;
			// var_dump($keywords);
			//得到的基础分类id
			$cate = $_POST['cate'];
			//其他分类id
			$other_cate = $_POST['other_cate'];
			if(empty($cate)||empty($other_cate)||empty($keywords)) {
				$this->error('请输入关键字或者选择分类','gettcnovel',2);
			}
			$Cate = D("novel_class")->where('class_id = '.$cate)->select();
			$OtherCate = D("novel_class")->where('class_id = '.$other_cate)->select();
			$cate_name = $Cate[0]['class_name'];//基础分类名字
			$other_class_name = $OtherCate[0]['class_name'];//其他分类名字
			$keywords = urlencode($keywords);
			// var_dump($keywords);die;
			// 请求的url，搜索的地址
			$surl = "https://c28.818tu.com/api/novels/search/?q=".$keywords;


			$header = fakeIp();
			$cookie = $_POST['cookie'];
			if (empty($cookie)) {
				$this->error('请填入cookie','gettcnovel',2);
			}
			$content = getUrlContent($surl,$header,$cookie,1);


            $content = json_decode($content,true);
            //dump($content);die;

			// echo "<pre>";
			// var_dump($content);die;
			$content = $content['data'][0];
			$novel_id = $content['id'];
			$data["name"] = $content["title"];
			$data["description"] = $content["summary"];
			//获取图片
			$img_urls = [];
			$img_urls['url'] = $content["avatar"];
			// 文件服务器接口地址
			$host = C('FILE_HOST_PATH');
			$curl_url = "$host/Home/File/uploadImg";
			// 请求远程地址 得到图片路径
			$path = curl_request($curl_url,$img_urls);
			$path = json_decode($path,true);
			$data["img"] = $path["path"];
			$data["mini_img"] = $path["path"];
			//类型
            $content['author'] = empty($content['author']) ? '':$content['author'];
			$data['writer'] = $content['author'];
			$data['is_over'] = $content['status'] == "completed" ? 1 : 0;
			$data['is_shelf'] = 0;
			$data['word_count'] = empty($content['words']) ? "100万" : $content['words']."字";
			$data["class_id"] = $cate;
			$data["class_name"] = $cate_name;
			$data["other_class_id"] = $other_cate;
			$data["other_class_name"] = $other_class_name;
			//来源
			$data["from"] = "同城群聊";
			$data["web_from"] = "https://c28.818tu.com/api/novels/catalog/".$content['id'];
			// 排重
//            dump($content['title']);
//            dump($_POST['keywords']);die;
			if ($content["title"] !== $_POST['keywords']) {
				die("搜索的小说不存在");
			}
			$map['name'] = $data['name'];
			$count = D("novel")->where($map)->count();
			if($count > 0){
				die("小说已经存在");
			}
		}
		//dump($data);die;
		$id = D("novel")->add($data);
		addlog('采集同城群聊小说');
		// 抓取小说信息结束 开始抓取章节
		$this->gettcchapter($id,$cookie,$data["name"],$novel_id,$data["mini_img"]);
	}

	/**
	 * @param  [type] $id         [小说表中的小说id]
	 * @param  [type] $cookie     [模拟浏览器的cookie]
	 * @param  [type] $novel_name [小说名字]
	 * @param  [type] $novel_id   [抓取的小说id]
	 * @param  [type] $img        [图片路径]
	 * @return [type]             [description]
	 */
	protected function gettcchapter($id,$cookie,$novel_name,$novel_id,$img)
	{
		set_time_limit(0);
		// 小说章节起始位置
		echo "<meta charset='utf-8'>";
		$start = 0;
			while(1){
				$header = fakeIp();
				$url = "https://c28.818tu.com/api/novels/catalog/".$novel_id."?start=".$start."fetch_novel=0";
				$chapter_list = getUrlContent($url,$header,$cookie,1);
				$chapter_list = json_decode($chapter_list,true);
				$chapter_list = $chapter_list['data']['catalog'];
				$add_data = array();
				$add_index = 0;
				foreach($chapter_list as $ckey=>$citem){
					$data["chapter"] = "第".$citem["idx"]."章";
					//过滤同城群聊的小说标题
					$title = $citem["title"];
					if(mb_strpos($title,"章",null,"UTF-8")){
						$pos_start = mb_strpos($title,"章",null,"UTF-8") + mb_strlen("章", "UTF-8");
						$title = mb_substr($title, $pos_start, (strlen($title) - $pos_start), "UTF-8");
						//过滤特别字符
						$title = str_replace(" ", "", $title);
						$title = str_replace("、", "", $title);
						$title = str_replace("·", "", $title);
						$title = str_replace(".", "", $title);
						$title = str_replace("（", "", $title);
						$title = str_replace("）", "", $title);
						$title = str_replace("：", "", $title);
						$title = str_replace(":", "", $title);
					}
					elseif(mb_strpos($title,"节",null,"UTF-8")){
						$pos_start = mb_strpos($title,"节",null,"UTF-8") + mb_strlen("节", "UTF-8");
						$title = mb_substr($title, $pos_start, (strlen($title) - $pos_start), "UTF-8");
						//echo $title." ".strlen($title)." ".$pos_start."<br>";

						//过滤特别字符
						$title = str_replace(" ", "", $title);
						$title = str_replace("、", "", $title);
						$title = str_replace("·", "", $title);
						$title = str_replace(".", "", $title);
						$title = str_replace("【", "", $title);
						$title = str_replace("】", "", $title);
						$title = str_replace("：", "", $title);
						$title = str_replace(":", "", $title);
					}
					else{
						//过滤数字
						for($i=0;$i<10;$i++)
							$title = str_replace("$i", "", $title);
						//过滤特别字符
						$title = str_replace(" ", "", $title);
						$title = str_replace("、", "", $title);
						$title = str_replace("·", "", $title);
						$title = str_replace(".", "", $title);
						$title = str_replace("【", "", $title);
						$title = str_replace("】", "", $title);
						$title = str_replace("（", "", $title);
						$title = str_replace("）", "", $title);
						$title = str_replace("：", "", $title);
						$title = str_replace(":", "", $title);
					}
					$data["title"] = $title;
					//echo $title;die;
					$data["chapter"] = "第".$citem["idx"]."章";
					$data["order_set"] = $citem["idx"];
					$data["add_time"] = date("Y-m-d H:i:s");

					//查询重复
					$c_map["order_set"] =  $citem["idx"];
					$c_map["novel_id"] = $item["id"];
					$count = D("novel_chapter")->where($c_map)->count();
					if($count > 0) {
						continue;
					}
					$header = fakeIp();
					$c_url = "https://c28.818tu.com/read/index/".$citem["id"]."?from=";
					$chapter_content = getUrlContent($c_url,$header,$cookie,1);
					$c_start = strpos($chapter_content, "<section>") + strlen("<section>");
					$c_end = strpos($chapter_content, "</section>", $c_start);
					$chapter_content = substr($chapter_content, $c_start, $c_end - $c_start);
					// var_dump($chapter_content);die;
					if(empty($chapter_content) || strlen($chapter_content) <= 0){
						echo "session失效了，id在$id";
						die;
					}
					$data["content"] = $chapter_content;
					$data['word_count'] = $this->runzishu($data['content']);
					//对应小说信息
					$data["novel_name"] = $novel_name;
					$data["novel_id"] = $id;
					$data["novel_mini_img"] = $img;

					$add_data[$add_index] = $data;
					$add_index++;
				}
				// print_r($add_data);die;
				if(!empty($add_data)) {
					D("novel_chapter_add")->addAll($add_data);
				}
				// 同步数据到主数据表
				$this->novelsync($id);
				// 判断章节数是否超过50条
				if(count($chapter_list) >= 50)
					$start += 50;
				else
					break;
			}
		echo "the end";die;
	}
	
	/**
	  *烈火小说抓取
	  */
	  public function getliehuo()
    {
        $class_model = M("NovelClass");
        $class_list = $class_model->where('is_other=0')->select();
        $other_list = $class_model->where('is_other=1')->select();
//        dump($other_list);die;
        $this->assign('class_list',$class_list);
        $this->assign('other_list',$other_list);
        $this->display();
    }

    public function getliehuo1()
    {
        //dump($_POST);die;

        set_time_limit(0);
        $kw = $_POST['keywords'];
        $cookie = $_POST['cookie'];
        //判断小说名是否存在
        $count = D('novel')->where("name='$kw'")->count();
        if ($count > 0) {
            $this->error('您搜索的小说已存在');
        }

        $keywords = urlencode($_POST['keywords']);

        $class_id = $_POST['class_id'];
        $other_class_id = $_POST['other_class_id'];
        if (empty($class_id) || empty($other_class_id)) {
            $this->error('请选择小说分类');
        }
        $class_model = M("NovelClass");
        $class_name_arr = $class_model->where("class_id=$class_id")->find();
        $other_class_arr = $class_model->where("class_id=$other_class_id")->find();


        $novel_name_url = 'https://lhxs.uyisi.co/novel/search?content=' . $keywords;

        $header = fakeIp();
        //王溪的cookie


        //获取网页的内容
        $content = getUrlContent($novel_name_url, $header, $cookie, 1);
        //echo $content;die;
        // dump($content);
        $book_id_url_start = strpos($content, '<a href="/novel/detail?book_id=') + strlen('<a href="/novel/detail?book_id=');
        $book_id_url_end = strpos($content, '"', $book_id_url_start);
        $book_id = substr($content, $book_id_url_start, $book_id_url_end - $book_id_url_start);

        //判断输入名字与搜索名字是否一致
        $book_name_start = strpos($content, '<div style="font-size: 16px;">') + strlen('<div style="font-size: 16px;">');
        $book_name_end = strpos($content, '</div>', $book_name_start);
        //小说名字
        $book_name = substr($content, $book_name_start, $book_name_end - $book_name_start);
        //dump($book_name);
        if ($kw != $book_name) {
            $this->error('您搜索的小说不存在');
        }
        //获取小说页面
        $book_url = 'https://lhxs.uyisi.co/novel/detail?book_id=' . $book_id;
        $bookcontent = getUrlContent($book_url, $header, $cookie, 1);
        //dump($bookcontent);
        //抓取小说基本信息
        //描述
        $description_start = strpos($bookcontent, '简介</span></h6>') + strlen('简介</span></h6>');
        $description_end = strpos($bookcontent, '</p>', $description_start);
        $description = substr($bookcontent, $description_start, $description_end - $description_start);
        //dump($description_start);dump($description_end); dump( $description);
        $description_true_tart = strpos($description, '<p>') + strlen('<p>');
        $description_true = substr($description, $description_true_tart);
        //dump($description_true);
        //图片
        //获取图片信息
        $img_url_start = strpos($bookcontent,'<img src="https://cdn.linexuc.com/')+strlen('<img src="');
        $img_url_end = strpos($bookcontent, '"', $img_url_start);
        $img_url = substr($bookcontent, $img_url_start, $img_url_end - $img_url_start);
        /*$img_url_start = strpos($bookcontent, 'https://cdn.linexuc.com/novel/static/book_img');
        $img_url_end = strpos($bookcontent, '"', $img_url_start);
        $img_url = substr($bookcontent, $img_url_start, $img_url_end - $img_url_start);*/
        $img_urls['url'] = $img_url;
        //使用文件服务器抓取图片
        $host = C('FILE_HOST_PATH');
        $curl_url = "$host/Home/File/uploadImg";
        $path = curl_request($curl_url, $img_urls);
        $path1 = json_decode($path, true);
        //dump($path1['path']);

        $book_lpc_start = strpos($bookcontent, '<div class="novel-info" style="padding-left:15px;padding-top:5px;">');
        $book_lpc_end = strpos($bookcontent, ' <div class="novel-summary block" style="padding: 15px;margin: 0;">');
        $book_lpc = substr($bookcontent, $book_lpc_start, $book_lpc_end - $book_lpc_start);

        $book_lpc_arr = explode('<div', $book_lpc);
        //dump($book_lpc_arr);

        //作者
        preg_match_all("/[\x{4e00}-\x{9fa5}]+/u", $book_lpc_arr[2], $writer_arr);
        //dump($writer_arr[0][0]);
        //完结
        if ($writer_arr[0][1] == '已完结') {
            $is_end = 1;
        } else {
            $is_end = 0;
        }

        //字数
        $word_count_start = strpos($book_lpc_arr[3], ' class="novel-meta" style="font-size:13px;padding:2 0;">') + strlen(' class="novel-meta" style="font-size:13px;padding:2 0;">');
        $word_count_end = strpos($book_lpc_arr[3], ' &nbsp', $word_count_start);
        $word_count = substr($book_lpc_arr[3], $word_count_start, $word_count_end - $word_count_start);
        //dump($word_count);
        $word_count = trim($word_count);
        //dump($word_count);
        //构造数组插入数据库
        $novel['name'] = $book_name;
        $novel['class_id'] = $class_id;
        $novel['class_name'] = $class_name_arr['class_name'];
        $novel['other_class_name'] = $other_class_arr['class_name'];
        $novel['other_class_id'] = $other_class_id;
        $novel['description'] = $description_true;
        $novel['img'] = $path1['path'];
        $novel['mini_img'] = empty($path1['path']) ? '' : $path1['path'];
        $novel['publish_time'] = date('Y-m-d H:i:s', time());
        $novel['word_count'] = $word_count;
        $novel['is_end'] = $is_end;
        $novel['from'] = '烈火小说';
        $novel['web_from'] = $book_url;
        $novel['is_shelf'] = 0;


        //dump($novel);
        $id = D('novel')->add($novel);
        if ($id) {
            $chapter_url = 'https://lhxs.uyisi.co/novel/chapter?book_id=' . $book_id . '&order=desc';
            $chapter_content = getUrlContent($chapter_url, $header, $cookie, 1);
            //dump($chapter_content);die;
            $chapter_list_start = strpos($chapter_content, '<ul class="text-novel-list">') + strlen('<ul class="text-novel-list">');
            $chapter_list_end = strpos($chapter_content, '</ul>', $chapter_list_start);
            $chapter_list_str = substr($chapter_content, $chapter_list_start, $chapter_list_end - $chapter_list_start);
            //dump($chapter_list_str);die;
            $chapter_list = explode('<li>', $chapter_list_str);
            //dump($chapter_list);die;
            array_shift($chapter_list);

            $data = array();
            $index = 0;

            foreach ($chapter_list as $key => $item) {
                //声明一个临时数组
                $tmp_arr = array();
                if ($index >= 50) {
                    //dump($data);
                    D('novel_chapter_add')->addAll($data);
                    //清空数据
                    $data = array();
                    $index = 1;
                }

                //抓取章节链接
                $chapter_a_start = strpos($item, ' <a href="') + strlen(' <a href="');
                $chapter_a_end = strpos($item, '"', $chapter_a_start);
                $chapter_a = substr($item, $chapter_a_start, $chapter_a_end - $chapter_a_start);
                $chapter_id = substr($chapter_a, strpos($chapter_a, '&chapter_id=') + strlen('&chapter_id='));


                $chapter_url = 'https://lhxs.uyisi.co' . $chapter_a;


                //获取章节具体内容
                $chapter_con = getUrlContent($chapter_url, $header, $cookie, 1);
                $title_start = strpos($chapter_con, '<h1 class="page_title">') + strlen('<h1 class="page_title">');
                $title_end = strpos($chapter_con, '</h1>', $title_start);
                $title_str = substr($chapter_con, $title_start, $title_end - $title_start);
                $title_arr_start = strpos($title_str,'章')+strlen('章');
                $title = trim(substr($title_str,$title_arr_start));
                $content_start = strpos($chapter_con, '<section>') + strlen('<section>');
                $content_end = strpos($chapter_con, '</section>', $content_start);
                $content_str = substr($chapter_con, $content_start, $content_end - $content_start);
                //dump($content_start);dump($content_end);
                $content_str = '<p>' . str_replace('<br><br>', '</p><p>', $content_str);
                $content_str = str_replace('<br/><br/>', '</p><p>', $content_str);
                $content_str = str_replace('</br></br>', '</p>', $content_str);
                $tmp_arr['chapter'] = '第' . $chapter_id . '章';
                $tmp_arr['title'] = empty($title) ? '' : $title;
                $tmp_arr['content'] = $content_str;
                $tmp_arr['word_count'] = $this->runzishu($tmp_arr['content']);
                $tmp_arr['novel_name'] = $book_name;
                $tmp_arr['novel_id'] = $id;
                $tmp_arr['novel_mini_img'] = empty($path1['path']) ? '' : $path1['path'];
                $tmp_arr['order_set'] = intval($chapter_id);
                $tmp_arr['add_time'] = date('Y-m-d H:i:s');

                //将临时数组赋值给$data
                $data[] = $tmp_arr;

//            //自动增加索引
                $index++;

            }
            //循环外面
            if (!empty($data)) {
                //dump($data);
                D("novel_chapter_add")->addAll($data);
            }

            //将所有数据插入主表中
            $sql = "insert into `qw_novel_chapter`(`chapter`,`title`,`content`,`novel_name`,`novel_id`,`novel_mini_img`,`order_set`,`add_time`,`word_count`) select `chapter`,`title`,`content`,`novel_name`,`novel_id`,`novel_mini_img`,`order_set`,`add_time`,`word_count` from qw_novel_chapter_add where  `novel_id`='$id';";
            D()->execute($sql);

            //echo $zhu_data;die('2');

            //echo $novel_id;die('1');
            //删除从表的数据
            D('novel_chapter_add')->where('novel_id='.$id)->delete();
            // echo $res2;die;


            echo '恭喜您,抓取完毕';
        }

    }

    //烈火小说更新
    public function updatenovel4(){
        set_time_limit(0);
        //获取烈火小说未完成的章节
        $where['is_end'] = 0;
        $where['from'] = '烈火小说';
        $novel_start = $_GET['novel_start'] ? $_GET['novel_start'] :0;
        $no_end_novel = D('novel')->where($where)->limit($novel_start,1)->select();
        //dump($no_end_novel);die;
        //dump($no_end_novel);die;
        $cookie = "open_id=oDn_z0nIzyFWGpNriazW7tDuo2s4; PHPSESSID=orqbgj9n96rlc4396s6uob4ou8; UM_distinctid=15e4ae5756efb-0eed67d6f-6a300224-1fa400-15e4ae5756f202; CNZZDATA1263456835=625185314-1504492159-%7C1504492159; Hm_lvt_1d0e183102a1dc5ddcf1c04df25e05e3=1504495106; Hm_lpvt_1d0e183102a1dc5ddcf1c04df25e05e3=1504495126";
        if (empty($no_end_novel)) {
            addlog("更新了全本小说");
            die('the end');
        }
        //dump($no_end_novel);die;
        //循环更新数据
        foreach ($no_end_novel as $key => $item){
            //小说id
            $novel_id = $item['id'];

            //网页链接地址
            $web_from = $item['web_from'];
//            echo $web_from;die;
//            echo $novel_id;die;s
            //根据小说去查找章节表的最后一章
            $where1['novel_id'] = $novel_id;

            //根据小说id获取最后一条章节的数据
            $last_chapter = D('novel_chapter')->where($where1)->order('id desc')->limit(1)->find();
            //dump($last_chapter);die;
//            dump($last_chapter);die;

            if(empty($last_chapter)){
                echo '小说为空';
                continue;
            }

            //数据库最后的章节
            $start = $last_chapter['order_set'];
//            echo $start;die;

            $add_data = array();
            $add_index = 0;
            while(1){
//                echo $web_from;die;
//                $url = $item["web_from"]."?start=".$start."&fetch_novel=0";
                $web_from_alllist = str_replace('detail','chapter',$web_from);
                $url = $web_from_alllist.'&chapter_id='.$start.'&order=asc';
                //dump($url);die;
                //获取抓取微信端全文小说的最后一章
                $header = fakeIp();
                //获取所有章节的内容
                $all_content = getUrlContent($url, $header, $cookie, 1);
                //echo $all_content;die;
                //dump($all_content);die;

                //preg_match_all('/\/book\/\d+\/(\d+)\.html/',$all_content,$lianjia);
                //最后一章的章节数
                $web_last_start = strripos($all_content,'第')+3;
                $web_last_end = strripos($all_content,'章');

                $web_last_chapter = substr($all_content,$web_last_start,$web_last_end-$web_last_start);
//                /dump($web_last_chapter);die;
                if($start == intval($web_last_chapter)){
                    echo $no_end_novel[$key]['name'].'已经是最新章节了'.'<br/>';
                }
                //开始采集的章节数
                $caiji_start = $start + 1;
                //$lianjie = array_shift($lianjia[0]);
                //dump($lianjie);die;
                //preg_match('/\/book\/(.*?)\/(.*?)\.html/',$lianjie,$chapter_start);
                //循环采集更新的小说
                //烈火小说内容域名前段
                $web_from_read = str_replace('detail','read',$web_from);
                for($i = $caiji_start; $i <= $web_last_chapter; $i++){

                    $lianjie_start_url = $web_from_read.'&chapter_id='.$i;
                    //echo $lianjie_start_url;echo '<br/>';
                    //得到需要采集的章节
                    $caiji_start_content = getUrlContent($lianjie_start_url, $header, $cookie, 1);
                    //echo $caiji_start_content;die;

                    //判断余额不足
                    preg_match('|书币余额不足|i',$caiji_start_content,$nomoney);
                    if(!empty($nomoney)){
                        echo '(您的账户余额不足,请及时充值)';die;
                    }

                    //真正的采集小说具体内容了
                    $tmp_arr = array();
                    $tmp_arr['chapter'] = '第'.$i.'章';
                    //获取标题
                    $title_start = strpos($caiji_start_content,'<div class="r-chaptername" id="lbChapterName">')+strlen('<div class="r-chaptername" id="lbChapterName">');
                    $title_end = strpos($caiji_start_content,'</div>',$title_start);
                    $title_count = substr($caiji_start_content,$title_start,$title_end-$title_start);
//                echo $title_count;echo '<br>';

                    if (strpos($title_count,'章')) {
                        $title_count_arr = explode('章',$title_count);
                        //dump($title_count_arr);
                        $title = array_pop($title_count_arr);
                        $title = trim($title);
                        $title = substr($title,0,strpos($title,'</'));
                        //dump($title);die;
                    }else{
                        $title = $title_count;
                        //echo $title;
                    }
                    //过滤特殊符号和不标准的字符串
                    if(strpos($title,'hapter')){
                        $title = ' ';
                    }
                    $title = specialSignRemove($title);
                    //dump($title);die;
                    $tmp_arr['title'] = isset($title) ? $title : '';

                    //获取小说内容
                    //dump($caiji_start_content);die;
                    $novel_content_start = strpos($caiji_start_content,'<section>')+strlen('<section>');
                    $novel_content_end = strpos($caiji_start_content,'</section>',$novel_content_start);
                    $novel_content = substr($caiji_start_content,$novel_content_start,$novel_content_end - $novel_content_start);
                    $startt_br = strpos($novel_content,'<br/><br/>');
                    if($startt_br == 0){
                        $novel_content = substr($novel_content,strlen('<br/><br/>'),strlen($novel_content) - 2*strlen('<br/><br/>'));
                    }
                    //替换标签
                    $novel_content = '<p>'.str_replace('<br/><br/>','</p><p>',$novel_content).'</p>';
                    //将小说内容装进$data中去
                    $tmp_arr['content'] = $novel_content;
                    $tmp_arr['word_count'] = $this->runzishu($tmp_arr['content']);
                    $tmp_arr['order_set'] = $i;
                    $tmp_arr["add_time"] = date("Y-m-d H:i:s");
                    $tmp_arr['novel_name'] = $no_end_novel[$key]['name'];
                    $tmp_arr['novel_id'] = $novel_id;
                    $tmp_arr['novel_mini_img'] = $no_end_novel[$key]['img'];
                    //dump($tmp_arr);die;
                    //查询重复章节,如果有就不插入没有就插入
                    $c_chapter['order_set'] = $i;
                    $c_chapter['novel_id'] = $novel_id;
                    //如果＞0,就说明章节重复
                    //dump($c_chapter);
                    $count = D('novel_chapter')->where($c_chapter)->count();
                    //dump($count);die;
                    if($count > 0 ){
                        continue;
                    }

                    $add_data[$add_index] = $tmp_arr;
                    $add_index++;
                }
                //dump($add_data);die;
                if(!empty($add_data)) {
                    //https://lhxs.uyisi.co/novel/detail?book_id=1002
                    /*$url = $web_from;
                    $all_content = getUrlContent($url, $header, $cookie, 1);
                    $end_str = substr($all_content,strpos($all_content,'|'),strpos($all_content,'</div>',strpos($all_content,'|'))-strpos($all_content,'|'));
                    preg_match("/[\x{4e00}-\x{9fa5}]+/u", $end_str, $is_end_arr);
                    if($is_end_arr[0] == '已完结'){
                        $end['is_end'] = 1;
                        $where_end['id'] = $novel_id;
                        D('novel')->where($where_end)->save($end);
                    }*/

                    $number = D('novel_chapter')->addAll($add_data);

                    //推送消息
                    if ($number > 0) {
                        //就证明更新了
                        //推送消息
                        $arrr = D('novel_chapter')->field('id,title,novel_id,novel_name,order_set,add_time')
                            ->order('id desc')->limit(1)->select();
                        foreach ($arrr as $arr){
                            $push_chapter_id = $arr['id'];
                            $push_novel_id = $arr['novel_id'];
                            $push_novel_name = $arr['novel_name'];
                            $push_title = $arr['title'];
                            $push_order_set = $arr['order_set'];
                            $push_add_time = $arr['add_time'];
                            $this->novelpush($push_novel_id,$push_chapter_id,$push_novel_name,$push_order_set,$push_title,$push_add_time);
                        }
                    }
                }
                //问题开始
                if( ($web_last_chapter - $caiji_start) < 50 ){
                    break;
                }else{
                    $start += 50;
                }
                //问题结束

            }
        }
        $domain = $_SERVER["HTTP_HOST"];
        $this->success('抓取下篇小说','http://'.$domain.'/Qwadmin/novel/updatenovel4?novel_start='.($novel_start + 1),1);

    }
	
	public function getgungun(){
        $class_model = M("NovelClass");
        $class_list = $class_model->where('is_other=0')->select();
        $other_list = $class_model->where('is_other=1')->select();
        $this->assign('class_list',$class_list);
        $this->assign('other_list',$other_list);
        $this->display();
    }


    public function getgungun1(){
        set_time_limit(0);
        //判断小说名是否存在
        $kw = $_POST['keywords'];
        $count = D('novel')->where("name='$kw'")->count();
        if($count > 0){
            $this->error('您搜索的小说已存在');
        }

        //判断参数是否传入
        $class_id = $_POST['class_id'];
        $other_class_id = $_POST['other_class_id'];
        if (empty($class_id) || empty($other_class_id)) {
            $this->error('请选择小说分类');
        }

        //抓取小说部分(1搜索页面链接)
        $keywords = urlencode($_POST['keywords']);
        $url = "https://m.gungunbook.com/index.php/Search/Index?keyword=$keywords";
        $header = fakeIp();
        $cookie ="PHPSESSID=bo5spaurokskaqp2an7o725lv4; UM_distinctid=15e4f9121b65d9-03c14478f-3a453759-1fa400-15e4f9121b7a38; click_num=24587; channel=0; city=m; user_id=6700681; CNZZDATA1262907365=810164596-1504573286-%7C1504573286; Hm_lvt_e913090a2d76f52398ce55c0239b170c=1504573466; Hm_lpvt_e913090a2d76f52398ce55c0239b170c=1504574325;";
        $content = getUrlContent($url,$header,$cookie,1);
        //获取具体小说的id
        $url_start = strpos($content,'<a href="/index.php/Book/Index/id/')+strlen('<a href="/index.php/Book/Index/id=');
        $url_end = strpos($content,'"',$url_start);
        $id = substr($content,$url_start,$url_end - $url_start);

        //抓取小说部分(2小说展示页面)
        $book_url = "https://m.gungunbook.com/index.php/Book/Index/id/$id";
        $book_content = getUrlContent($book_url,$header,$cookie,1);
        //dump($book_content);die;
        //获取小说名
        $book_name_start = strpos($book_content,'<a href="javascript:;" class="book-name">')+strlen('<a href="javascript:;" class="book-name">');
        $book_name_end = strpos($book_content,"</a>",$book_name_start);
        $book_name = substr($book_content,$book_name_start,$book_name_end-$book_name_start);
        if($kw != $book_name) {
            $this->error('您搜索的小说不存在');
        }
        //获取小说作者
        $book_write_start = strpos($book_content,'<a href="javascript:;"><span class="author-name">')+strlen('<a href="javascript:;"><span class="author-name">');
        $book_write_end = strpos($book_content,'</span>',$book_write_start);
        $book_write = substr($book_content,$book_write_start,$book_write_end-$book_write_start);
        //获取小说字数(千万别改也别缩进)
        $book_count_start = strpos($book_content,'</span></a>
		 		<p>')+strlen('</span></a>
		 		<p>');
        $book_count_end = strpos($book_content,'</p>
		 		<p><span>',$book_count_start);
        $book_count = substr($book_content,$book_count_start,$book_count_end-$book_count_start);
        //小说图片
        $book_img_start = strpos($book_content,'<img class="book-cover" src="')+strlen('<img class="book-cover" src="');
        $book_img_end = strpos($book_content,'" alt="" width="90">',$book_img_start);
        $book_img = substr($book_content,$book_img_start,$book_img_end-$book_img_start);
        $img_urls['url'] = $book_img;
        //小说图片传入文件服务器
        //使用文件服务器抓取图片
        $host = C('FILE_HOST_PATH');
        $curl_url = "$host/Home/File/uploadImg";
        $path = curl_request($curl_url,$img_urls);
        $path1 = json_decode($path,true);
        //dump($path1);die;
        //小说简介
        $description_start = strpos($book_content,'<div class="abstract"> <div class="abstract-content con-hidden">')+strlen('<div class="abstract"> <div class="abstract-content con-hidden">');
        $description_end = strpos($book_content,'</div>',$description_start);
        $description = substr($book_content,$description_start,$description_end-$description_start);

        //查找数据
        $class_model = M("NovelClass");
        $class_name_arr = $class_model->where("class_id=$class_id")->find();
        $other_class_arr = $class_model->where("class_id=$other_class_id")->find();

        //novel数据入库
        $novel['name'] = $book_name;
        $novel['class_id'] = $class_id;
        $novel['class_name'] = $class_name_arr['class_name'];
        $novel['other_class_name'] = $other_class_arr['class_name'];
        $novel['other_class_id'] = $other_class_id;
        $novel['description'] = $description;
        $novel['img'] = $path1['path'];
        $novel['mini_img'] = empty($path1['path']) ? '' : $path1['path'];
        $novel['publish_time'] = date('Y-m-d H:i:s',time());
        $novel['word_count'] = $book_count;
        $novel['is_end'] = 0;
        $novel['from'] = '滚滚小说';
        $novel['write'] = $book_write;
        $novel['web_from'] = $book_url;
        $novel['is_shelf'] = 0;
        $book_id = D('novel')->add($novel);

        //插入book_chapter表
        if ($book_id) {
            $chapter_url = "https://m.gungunbook.com/index.php/Book/Directory/id/$id";
            $chapter_content = getUrlContent($chapter_url,$header,$cookie,1);
            //dump($chapter_content);die;
            //网页总页数
			$total_page_start = strpos($chapter_content, '<select class="page-select" id="listPage">') + strlen('<select class="page-select" id="listPage">');
			$total_page_end = strpos($chapter_content, '</select>', $total_page_start);
			$total_page = substr($chapter_content, $total_page_start, $total_page_end - $total_page_start);
			$total = explode('<option', $total_page);
			array_shift($total);
			$total_page = count($total);
            //dump($total_page);die;
            //分页循环
            $ii = 0;
            for($i =1; $i<=$total_page; $i++){
                $pageurl = "https://m.gungunbook.com/index.php/Book/Directory/id/$id/page/$i";
                $chapterpage = getUrlContent($pageurl,$header,$cookie,1);
                $counts_start = strpos($chapterpage,'<ul class="lists">')+strlen('<ul class="lists">');
                $counts_end = strpos($chapterpage,'</ul>');
                $content_str = substr($chapterpage,$counts_start,$counts_end-$counts_start);
                $content_arr = explode('<li',$content_str);
                array_shift($content_arr);
                foreach($content_arr as $key=>$item){
                    //获取章节链接
                    $href_start = strpos($item,'href="')+strlen('href="');
                    $href_end = strpos($item,'">',$href_start);
                    $href =substr($item,$href_start,$href_end-$href_start);

                    $htmlurl ="https://m.gungunbook.com$href";

                    //获取章节具体内容
                    $chapter_con = getUrlContent($htmlurl,$header,$cookie,1);
                    //dump($chapter_con);die;
                    //章节chapter  title

                    $chapter_start = strpos($chapter_con,'<p class="chapter-name">')+strlen('<p class="chapter-name">');
                    $chapter_end = strpos($chapter_con,'</p>',$chapter_start);
                    $chapter_lr_str = substr($chapter_con,$chapter_start,$chapter_end-$chapter_start);
                    $chapter_lr_array = explode(' ',$chapter_lr_str);
                    //获取章节
                    $chapter_end_1 = strpos($chapter_lr_str,'章')+strlen('章');
                    $chapter = substr($chapter_lr_str,0,$chapter_end_1);
                    $chapter = trim($chapter);
                    //获取标题
                    $title = substr($chapter_lr_str,$chapter_end_1);
                    $title = trim($title);
                    $one = substr($chapter,0,3);
                    if( $one == '第'){
                        $map['chapter'] = $chapter;
                        $map['title'] = $title;
                        //章节内容
                        $chapter_p_start = strpos($chapter_con,'<div class="chapter-con">')+strlen('<div class="chapter-con">');
                        $chapter_p_end = strpos($chapter_con,'</div>',$chapter_p_start);
                        $chapter_p_str = substr($chapter_con,$chapter_p_start,$chapter_p_end-$chapter_p_start);
                        $map['content'] = $chapter_p_str;
                        $map['word_conut'] = $this->runzishu($map['content']);
                        //该章节其他数据
                        $map['novel_name'] = $book_name;
                        $map['novel_id'] = $book_id;
						$os = 50 * ($i - 1) + $key + 1 - $ii;
						$map['chapter'] = '第'.$os.'章';
                        $map['novel_mini_img'] = empty($path1['path']) ? '' : $path1['path'];
                        $map['order_set'] = 50*($i-1)+$key+1-$ii;
                        $map['add_time'] = date('Y-m-d H:i:s');
						$map['word_count'] = $this->runzishu($map['content']);
                        D("novel_chapter_add")->add($map);
                    }else{
                        $ii++;
                        continue;
                    }


                }

            }
            //将所有数据插入主表中
            $sql = "insert into `qw_novel_chapter`(`chapter`,`title`,`content`,`novel_name`,`novel_id`,`novel_mini_img`,`order_set`,`add_time`,`word_count`) select `chapter`,`title`,`content`,`novel_name`,`novel_id`,`novel_mini_img`,`order_set`,`add_time`,`word_count` from qw_novel_chapter_add where  `novel_id`='$book_id';";
            D()->execute($sql);
            //删除从表的数据
            D('novel_chapter_add')->where('novel_id='.$book_id)->delete();

        }
		 echo '恭喜您,抓取完毕';

    }
	

	
	
	
	//跑出novel_chapter中的字数
    /*public function chapter_count(){
        set_time_limit(0);
        $chapter_count = D('novel_chapter')->field('id,content')->select();
        foreach ($chapter_count as $key=>$item){
            //$map['world_content'] = mb_strlen($item['content'],'utf-8');
            preg_match_all("/[\x{4e00}-\x{9fa5}]+/u", $item['content'], $content_arr);
            $map['word_count'] = mb_strlen(implode('',$content_arr[0]),'utf-8');
            $map_s['id'] = $item['id'];
            D('novel_chapter')->where($map_s)->save($map);
        }
        echo '跑完了';
    }*/
	
	
		/**
	 ** 滚滚小说更新
	 */
	public function updatenovel5()
	{
		set_time_limit(0);
		//获取全本小说未完结的小说的信息
		$where['is_end'] = 0;
		$where['from'] = '滚滚小说';
		$novel_start = $_GET['novel_start'] ? $_GET['novel_start'] : 0;
		$no_end_novel = D('novel')->where($where)->limit($novel_start, 1)->select();
		if (empty($no_end_novel)) {
			addlog("已更新了滚滚小说");
			die('the end');
		}

		$header = fakeIp();
		$cookie = "PHPSESSID=bo5spaurokskaqp2an7o725lv4;
				UM_distinctid=15e4f9121b65d9-03c14478f-3a453759-1fa400-15e4f9121b7a38;
				click_num=24587; channel=0; city=m; user_id=6700681;
				CNZZDATA1262907365=810164596-1504573286-%7C1504573286;
				Hm_lvt_e913090a2d76f52398ce55c0239b170c=1504573466;
				Hm_lpvt_e913090a2d76f52398ce55c0239b170c=1504574325;";

		foreach ($no_end_novel as $key => $item) {
			$novel_id = $item['id'];
			$map['novel_id'] = $item['id'];
			$web_from = $item['web_from'];
			$name = $item['name'];
			$mini_img = $item["mini_img"];
			preg_match_all('/[1234567890]/', $web_from, $id);
			$id = implode('', $id[0]);
			//根据小说去查找章节表的最后一章
			$last_chapter = D('novel_chapter')->where($map)->order('id desc')->limit(1)->find();
			if (empty($last_chapter)) {
				echo '小说为空';
				continue;
			}

			$last_order_set = $last_chapter['order_set'];
			$last_title = $last_chapter['title'];
			$chapter_url = "https://m.gungunbook.com/index.php/Book/Directory/id/$id";
			$chapter_content = getUrlContent($chapter_url, $header, $cookie, 1);
			$page = intval($last_order_set / 50) + 1;

			//网页总页数
			$total_page_start = strpos($chapter_content, '<select class="page-select" id="listPage">') + strlen('<select class="page-select" id="listPage">');
			$total_page_end = strpos($chapter_content, '</select>', $total_page_start);
			$total_page = substr($chapter_content, $total_page_start, $total_page_end - $total_page_start);

			$total = explode('<option', $total_page);
			array_shift($total);
			$total_page = count($total);
			//计算需要跟新的链接
			for ($i = $page; $i <= $total_page; $i++) {
				//分页链接
				$chapter_page_url[$i] = "https://m.gungunbook.com/index.php/Book/Directory/id/$id/page/$i/desc/1";
			}
			$index = 0;
			$data = array();
			//根据目录分别找到内容入数据库
			$ii = 1;
			foreach ($chapter_page_url as $key => $item) {
				$content = getUrlContent($item, $header, $cookie, 1);
				//dump($content);
				//获取章节链接
				$counts_start_one = strpos($content, '<ul class="lists">') + strlen('<ul class="lists">');
				$counts_end_one = strpos($content, '</ul>');
				$content_str_one = substr($content, $counts_start_one, $counts_end_one - $counts_start_one);
				$content_arr_one = explode('<li', $content_str_one);
				array_shift($content_arr_one);
				//dump($content_arr_one);
				foreach($content_arr_one as $key=>$vv){
					if(strstr($vv,$last_title)) {
						$a = $key + 1;
						$content_arr_one = array_slice($content_arr_one,$a);
					}
				}
				foreach ($content_arr_one as $vv) {
					$href_start_one = strpos($vv, 'href="') + strlen('href="');
					$href_end_one = strpos($vv, '">', $href_start_one);
					$href_one = substr($vv, $href_start_one, $href_end_one - $href_start_one);
					$htmlurl_one = "https://m.gungunbook.com$href_one";
					//文章链接
					$book_cookie = "nowNum=2; PHPSESSID=4tpfr8l3nncsl6r6ct5kh10pl2; UM_distinctid=15e55b3b18268a-0c89f78fd-3a453759-1fa400-15e55b3b183962; click_num=24587; channel=0; city=m; user_id=6700681; read_bg=1; read_size=1; bookNum=24587; chapterNum=122304; read_num=122304; CNZZDATA1262907365=1488565702-1504672477-%7C1504677890; Hm_lvt_e913090a2d76f52398ce55c0239b170c=1504676394; Hm_lpvt_e913090a2d76f52398ce55c0239b170c=1504682736;";
					$chapter_con_one = getUrlContent($htmlurl_one, $header, $book_cookie, 1);
					//dump($chapter_con_one);die;
					$is_success = strpos($chapter_con_one, '<link rel="stylesheet" href="https://static.gungunbook.com//wechat/M/css/reward.css?v=20170731" />');
					if (empty($is_success)) {
						echo $chapter_con_one;
						die("内容没找到，抓取不了");
					}
					if (strlen($chapter_con_one) == 0)
						die("请充值");
					//章节chapter  title
					$chapter_start_one = strpos($chapter_con_one, '<p class="chapter-name">') + strlen('<p class="chapter-name">');
					$chapter_end_one = strpos($chapter_con_one, '</p>', $chapter_start_one);
					$chapter_lr_str_one = substr($chapter_con_one, $chapter_start_one, $chapter_end_one - $chapter_start_one);
					//获取章节
					$chapter_end_1_one = strpos($chapter_lr_str_one, '章') + strlen('章');
					$chapter_one = substr($chapter_lr_str_one, 0, $chapter_end_1_one);
					$chapter1 = trim($chapter_one);
					//获取标题
					$title_one = substr($chapter_lr_str_one, $chapter_end_1_one);
					$title1 = trim($title_one);
					$map['title'] = $title1;
					/*$count = D("novel_chapter")->where($map)->count();
					if ($count > 0)
						continue;*/

					if (substr($chapter1, 0, 3) == '第') {
						//获取order_set
						$order_set = $last_order_set + $ii;

						//标题赋值
						$data["chapter"] = $chapter1 ? '第' . $order_set . '章' : " ";
						$data["title"] = $title1 ? $title1 : " ";
						//获取章节内容
						$chapter_p_start = strpos($chapter_con_one, '<div class="chapter-con">') + strlen('<div class="chapter-con">');
						$chapter_p_end = strpos($chapter_con_one, '</div>', $chapter_p_start);
						$data["content"] = substr($chapter_con_one, $chapter_p_start, $chapter_p_end - $chapter_p_start);
						$zishu = substr($chapter_con_one, $chapter_p_start, $chapter_p_end - $chapter_p_start);
						$data['word_count'] = $this->runzishu($zishu);
						//设置数据
						$data["novel_name"] = $name;
						$data["novel_id"] = $novel_id;
						$data["novel_mini_img"] = $mini_img;
						$data["add_time"] = date("Y-m-d H:i:s");
						$data['order_set'] = $last_order_set + $ii;
						$ii++;
						//dump($data);
						//索引自动增长
						D('novel_chapter')->add($data);
					}

				}
			}

		}
		$domain = $_SERVER["HTTP_HOST"];
		$this->success('抓取下篇小说', 'http://' . $domain . '/Qwadmin/novel/updatenovel5?novel_start=' . ($novel_start + 1), 1);
	}
	
	
	
	public function chapter_count(){
        set_time_limit(0);


        $count = D('novel_chapter')->count();
        //传入总条数
        $pram = $_GET['pram']?$_GET['pram']:0;
        if ($pram<=0){
            $pram = 1;
        }
        $total_start = intval($pram-1)*10000;

        if ($pram > ceil($count/10000)){
            echo '你给的parm不符合要求！';die;
        }elseif($pram == ceil($count/10000)){
            $total_num = $count - $pram*10000;
        }else{
            $total_num = 10000;
        }
        $start = $_GET['start']?$_GET['start']:$total_start;
        if($total_num*$pram < $start){
            echo '程序跑完了';
            die;
        }
        //$start 最终页数
        $page_end = ceil($total_num/100);
        $chapter_count = D('novel_chapter')->field('id,content')->order('id desc')->limit($start,100)->select();
        foreach ($chapter_count as $key=>$item){
            //$map['world_content'] = mb_strlen($item['content'],'utf-8');
            preg_match_all("/[\x{4e00}-\x{9fa5}]+/u", $item['content'], $content_arr);
            $map['word_count'] = mb_strlen(implode('',$content_arr[0]),'utf-8');
            $map_s['id'] = $item['id'];
            D('novel_chapter')->where($map_s)->save($map);
        }

        
        $starts = $start+100;
        $domain = $_SERVER['HTTP_HOST'];
        $this->success('继续下一百条','http://'.$domain.'/Qwadmin/Novel/chapter_count.html?pram='.$pram.'&start='.$starts);
        //echo '跑完了';
    }

    public function word(){
        //  echo 1;
        set_time_limit(0);
        //dump($_GET);
        $start = empty($_GET['start'])?0:$_GET['start'];


        //dump($start);die;
        $end = $_GET['end'];
        //$end = $end*1000;
        //dump($end);die;
        if($start == $end){
            die('已完成');
        }
        $chapter_arr = D('novel_chapter')->limit("$start,1")->select();
        //dump($chapter_arr);die;
        if(empty($chapter_arr)){
            echo 'the end';die;
        }
        //dump($chapter_arr);

        foreach ($chapter_arr as $value){
            $id = $value['id'];
            $content = $value['content'];
            $word_count = mb_strlen($content,'utf-8');
            $map['word_count'] = 3;
            D('novel_chapter')->where("id=$id")->save($map);
        }
        $start1 = $start + 1;
        $this->success('改下一百条',U('Novel/word','start='.$start1.'&end='.$end));

    }

    /**
     * 计算每一章节字数
     */
    public function runzishu($content){
        preg_match_all("/[\x{4e00}-\x{9fa5}]+/u", $content, $content_arr);
        $word = mb_strlen(implode('',$content_arr[0]),'utf-8');
        return $word;
    }
	
	/**
	  *跑开始关注章节和开始收费章节
	  */
	  public function runfree(){
        //首先取出全部的novel
        $novel_arr = D('novel')->field('id')->select();
        foreach($novel_arr as $key =>$value){

            //然后根据novel_id去chapter里面找对应的章节数量
            $novel_id = $value['id'];
            //找到章节数量之后按照对应的规则加字段
            $count = D('novel_chapter')->field('order_set')->distinct(true)->where("novel_id='$novel_id'")->count();
            //dump($count);

            //规则如下
            //200章以内  第九章收费   推广链接从第5章开始关注
            if($count < 200){
                $map['free_chapter'] = 9;
                $map['subscribe_chapter'] = 3;
            }elseif ($count>=200 && $count<=500){
                $map['free_chapter'] = 14;
				$map['subscribe_chapter'] = 8;
            }else{
                $map['free_chapter'] = 18;
				$map['subscribe_chapter'] = 8;
            }
            D('novel')->where("id='$novel_id'")->save($map);
            //200-500章   第十四章收费   推广链接从第7章开始关注
            //500以上   第十八章收费   推广链接从第8章开始关注
        }
        echo 'the end';

    }

    /**
     *不全数据库中word_count为 0 的情况
     *
     */
    public function word_zero(){
        $word_date = D('novel_chapter')->field('id')->where('word_count = 0')->select();
        //dump($word_date);die;
        foreach ($word_date as $val){
            $content = D('novel_chapter')->field('content')->where('id='.$val['id'])->find();
            preg_match_all("/[\x{4e00}-\x{9fa5}]+/u", $content['content'], $content_arr);
            $word = mb_strlen(implode('',$content_arr[0]),'utf-8');
            $maps['word_count'] = $word;
            dump($maps['word_count']);
            D('novel_chapter')->where('id='.$val['id'])->save($maps);
        }
        die('ok');
    }
	
	
	
	
		/**
	 * 指悦书吧
	 */
	public function getnashou(){
		$class_model = M("NovelClass");
		$class_list = $class_model->where('is_other=0')->select();
		$other_list = $class_model->where('is_other=1')->select();
		$this->assign('class_list',$class_list);
		$this->assign('other_list',$other_list);
		$this->display();
	}


	/**
	 * 只能对那手，我下针抓取
	 */
	public function getnashou1(){
		set_time_limit(0);
		$kw = $_POST['keywords'];

		//判断小说名是否存在
		$count = D('novel')->where("name='$kw'")->count();
		/*if($count > 0){
			$this->error('您搜索的小说已存在');
		}*/
		//获取分类内容
		$class_id = $_POST['class_id'];
		$other_class_id = $_POST['other_class_id'];
		if (empty($class_id) || empty($other_class_id)) {
			$this->error('请选择小说分类');
		}
		$class_model = M("NovelClass");
		$class_name_arr = $class_model->where("class_id=$class_id")->find();
		$other_class_arr = $class_model->where("class_id=$other_class_id")->find();
		//获取当前小说列表（获得总页数）
		$header = fakeIp();
		$cookie = "JSESSIONID=D7ECE54E186322E7E09985D57283EB5C;
					channelCode=K917238; colorsVal=3;
					contentJson=%7B%22bookNight%22%3Afalse%2C%22fontSize%22%3A%2218px%22%2C%22pageShow%22%3Afalse%2C%22defaultSwitch%22%3Afalse%2C%22bookBg%22%3A%7B%22selectVal%22%3A2%2C%22aAookBg%22%3A%5B%22book_bg_1%22%2C%22book_bg_2%22%2C%22book_bg_3%22%2C%22book_bg_4%22%5D%7D%7D;
					Hm_lvt_a4c87b74a3c51ec8dc419df544fab2a5=1504832710;
					Hm_lpvt_a4c87b74a3c51ec8dc419df544fab2a5=1504835475";
		$mulu_url = "http://book.zhiyuekj.com/Weixingzhxs/Book_catalogue_Servlet?userid=42646&sign=568381D4189C9176170A49D2A0BFCF3F&bookid=1&pageNumber=1&pageSize=100";
		$mulu_json = getUrlContent($mulu_url,$header,$cookie,1);
		$muli_content = json_decode($mulu_json,true);
		//dump($muli_content);die;
		$total_chapter = $muli_content['result']['total'];
		$total_page = ceil($total_chapter/100);
		//入novel表
		$map['name'] = '那手，我下针';
		$map['description'] = '大学生依靠祖龙凤诀，一手独到推拿医术，让无数美女竞相倒贴...';
		//图片处理
		$img_urls['url'] ="http://www.mixs.cn/files/article/image/147/147181/147181s.jpg";
		//上传到文件服务器
		$host = C('FILE_HOST_PATH');
		$curl_url = "$host/Home/File/uploadImg";
		$img_json = curl_request($curl_url,$img_urls);
		//$img_json=substr($img_json,3);
		$path = json_decode($img_json,true);
		$map['img'] = $path['path'];
		$map['mini_img'] = empty($path['path']) ? '' : $path['path'];;
		$map['class_id'] = $class_id;
		$map['class_name'] =  $class_name_arr['class_name'];
		$map['other_class_id'] = $other_class_id;
		$map['other_class_name'] = $other_class_arr['class_name'];
		$map['publish_time'] = date('Y-m-d H:i:s',time());
		$map['word_count'] = '10万';
		$map['publish_time'] = date('Y-m-d H:i:s',time());
		$map['is_end'] = 0;
		$map['is_shelf'] = 0;
		$map['from'] = '指悦书吧';
		$map['web_from'] = "http://book.zhiyuekj.com/Weixingzhxs/Book_booksparti_Servlet?userid=42646&sign=568381D4189C9176170A49D2A0BFCF3F&bookid=1&pageNumber=1&pageSize=4";
		$map['write'] = '';
		$id = D('novel')->add($map);
		if($id){
			for($i = 1;$i<= $total_page;$i++){
				$every_url = "http://book.zhiyuekj.com/Weixingzhxs/Book_catalogue_Servlet?userid=42646&sign=568381D4189C9176170A49D2A0BFCF3F&bookid=1&pageNumber=$i&pageSize=100";
				$every_json = getUrlContent($every_url,$header,$cookie,1);
				$every_content = json_decode($every_json,true);
				$chapter_array = $every_content['result']['chapterdir'];
				foreach($chapter_array as $chapter){
					//获取每一章节的内容
					$userid = 42646;
					$chapterid = $chapter['chapterid'];
					$bookid = $chapter['bookid'];
					$key = substr('9D542EB06EB07AD84803963A547515D4',15);
					$sign =strtoupper(md5($key.'bookid'.$bookid.'chapterid'.$chapterid."userid".$userid.$key));
					$chapter_url = "http://book.zhiyuekj.com/Weixingzhxs/Book_logdreader_Servlet?userid=42646&sign=$sign&bookid=$bookid&chapterid=$chapterid";
					$chapter_json = getUrlContent($chapter_url,$header,$cookie,1);
					$chapter_lr = json_decode($chapter_json,true);
					//数据处理
					$chapter_content1 = "<p>".trim($chapter_lr['content'])."</p>";
					$order= array("    \r\n\r\n   ");
					//获取chapter内容content;
					$chpter_content = str_replace($order,'</p><p>',$chapter_content1);
					//获取chapter名字
					$chapter_name = $chapter_lr['chapterName'];
					$chapterName = explode(' ',$chapter_name);
					$data['chapter'] = $chapterName[0];
					$data['title'] = $chapterName[1];
					$data['content'] = $chpter_content;
					$data['novel_name'] = '那手，我下针';
					$data['novel_id'] = $id;
					$data['novel_mini_img'] = $path['path'];
					$title = $chapterName[0];
					preg_match_all('/[0123456789]/',$title,$match);
					$match = implode('',$match[0]);
					$data['order_set'] = $match;
					$data['add_time'] = date('Y-m-d H:i:s');
					$data['word_count'] = $chapter_lr['wordNum'];
					D("novel_chapter_add")->add($data);
				}
			}
			//将所有数据插入主表中
			$sql = "insert into `qw_novel_chapter`(`chapter`,`title`,`content`,`novel_name`,`novel_id`,`novel_mini_img`,`order_set`,`add_time`,`word_count`) select `chapter`,`title`,`content`,`novel_name`,`novel_id`,`novel_mini_img`,`order_set`,`add_time`,`word_count` from qw_novel_chapter_add where  `novel_id`='$id';";
			D()->execute($sql);
			//删除从表的数据
			//D('novel_chapter_add')->where('novel_id='.$id)->delete();
		}



	}
	
		/**
		*针对那手更新
		*/
	
		public function updatenovel6(){
		set_time_limit(0);
		//获取全本小说未完结的小说的信息
		$where['is_end'] = 0;
		$where['from'] = '指悦书吧';
		$novel_start = $_GET['novel_start'] ? $_GET['novel_start'] :0;
		//查找滚滚小说没有更新完的小说
		$no_end_novel = D('novel')->where($where)->limit("$novel_start,1")->select();
		if(empty($no_end_novel)){
			addlog("已更新了指悦小说");
			die('the end');
		}
		//仅针对那手，我下针进行抓取
		foreach($no_end_novel as $item){
			//dump($item);
			//获取当前小说列表（获得总页数）
			$header = fakeIp();
			$cookie = "JSESSIONID=D7ECE54E186322E7E09985D57283EB5C;
					channelCode=K917238; colorsVal=3;
					contentJson=%7B%22bookNight%22%3Afalse%2C%22fontSize%22%3A%2218px%22%2C%22pageShow%22%3Afalse%2C%22defaultSwitch%22%3Afalse%2C%22bookBg%22%3A%7B%22selectVal%22%3A2%2C%22aAookBg%22%3A%5B%22book_bg_1%22%2C%22book_bg_2%22%2C%22book_bg_3%22%2C%22book_bg_4%22%5D%7D%7D;
					Hm_lvt_a4c87b74a3c51ec8dc419df544fab2a5=1504832710;
					Hm_lpvt_a4c87b74a3c51ec8dc419df544fab2a5=1504835475";
			//该目录为那手的目录，如需更改，替换sign 和bookid
			$mulu_url = "http://book.zhiyuekj.com/Weixingzhxs/Book_catalogue_Servlet?userid=42646&sign=568381D4189C9176170A49D2A0BFCF3F&bookid=1&pageNumber=1&pageSize=100";
			$mulu_json = getUrlContent($mulu_url,$header,$cookie,1);
			$muli_content = json_decode($mulu_json,true);
			//dump($muli_content);die;
			$total_chapter = $muli_content['result']['total'];
			$total_page = ceil($total_chapter/100);
			$novel_name = $item['name'];
			//查找数据库最新数据
			$last_chapter = D('novel_chapter')->where("novel_name = '$novel_name'")->order('id desc')->limit(1)->find();
			$last_order_set = $last_chapter['order_set'];
			if($last_order_set == $total_chapter){
				echo '已经为最新章节';die;
			}
			//更新小说
			$curl_page = intval($last_order_set/100) +1;

			for($i = $curl_page;$i<= $total_page;$i++){
				$every_url[$i] = "http://book.zhiyuekj.com/Weixingzhxs/Book_catalogue_Servlet?userid=42646&sign=568381D4189C9176170A49D2A0BFCF3F&bookid=1&pageNumber=$i&pageSize=100";
			}
			foreach($every_url as $ml_url){
				$every_json = getUrlContent($ml_url,$header,$cookie,1);
				$every_content = json_decode($every_json,true);
				$chapter_array = $every_content['result']['chapterdir'];
				foreach($chapter_array as $chapter){
					$chapter_title = explode(' ',$chapter['chapterName']);
					$m_chapter = $chapter_title['0'];
					$m_title = $chapter_title['1'];
					$count = D('novel_chapter')->where("title = '$m_title' and chapter = '$m_chapter'")->count();
					if($count >0)
						continue;
					//获取每一章节的内容
					$userid = 42646;
					$chapterid = $chapter['chapterid'];
					$bookid = $chapter['bookid'];
					$key = substr('9D542EB06EB07AD84803963A547515D4',15);
					$sign =strtoupper(md5($key.'bookid'.$bookid.'chapterid'.$chapterid."userid".$userid.$key));
					$chapter_url = "http://book.zhiyuekj.com/Weixingzhxs/Book_logdreader_Servlet?userid=42646&sign=$sign&bookid=$bookid&chapterid=$chapterid";

					$chapter_json = getUrlContent($chapter_url,$header,$cookie,1);
					$chapter_lr = json_decode($chapter_json,true);
					if(empty($chapter_lr['content'])) {
						die("内容没找到，抓取不了");
					}
					if(strlen($chapter_lr['content']) == 0)
						die("请充值");
					//数据处理
					$chapter_content1 = "<p>".trim($chapter_lr['content'])."</p>";
					$order= array("\r\n\r\n ");
					//获取chapter内容content;
					$chpter_content = str_replace($order,'</p><p>',$chapter_content1);
					//获取chapter名字
					$chapter_name = $chapter_lr['chapterName'];
					$chapterName = explode(' ',$chapter_name);
					$data['chapter'] = $chapterName[0];
					$data['title'] = $chapterName[1];
					$data['content'] = $chpter_content;
					$data['novel_name'] = '那手，我下针';
					$data['novel_id'] = $item['id'];
					$data['novel_mini_img'] = $item['img'];
					$title = $chapterName[0];
					preg_match_all('/[0123456789]/',$title,$match);
					$match = implode('',$match[0]);
					$data['order_set'] = $match;
					$data['add_time'] = date('Y-m-d H:i:s');
					$data['word_count'] = $chapter_lr['wordNum'];
					//dump($data);die;
					D("novel_chapter")->add($data);
				}
			}
			echo '本次更新完成';die;
		}
		//$domain = $_SERVER["HTTP_HOST"];
		//$this->success('抓取下篇小说','http://'.$domain.'/Qwadmin/novel/updatenovel6?novel_start='.($novel_start + 1),1);
	}

    //session表 跑open_id_pre
    public function runsession(){
        set_time_limit(0);
        $open_id_arr= D('session')->field('open_id,id,nickname,open_id_pre')->where("open_id != '' and open_id_pre = ''")->select();
        //dump($open_id_arr);die;
        foreach ($open_id_arr as $value) {
            $open_id_pre = substr($value['open_id'], 0, 6);
            $user_id = $value['id'];
            $map['open_id_pre'] = $open_id_pre;
            //dump($map['open_id_pre']);
            D('session')->where("id='$user_id'")->save($map);
        }
        header("Content-type:text/html;charset=utf-8");
        echo 'session表数据跑完!!';
    }

    //user表 跑open_id_pre
    public function runuser(){
        set_time_limit(0);
        $open_id_arr= D('user')->field('open_id,user_id,nickname,open_id_pre')->where("open_id != '' and open_id_pre = ''")->select();
        //dump($open_id_arr);die;
        foreach ($open_id_arr as $value) {
            $open_id_pre = substr($value['open_id'], 0, 6);
            $user_id = $value['user_id'];
            $map['open_id_pre'] = $open_id_pre;
            //dump($map['open_id_pre']);
            D('user')->where("user_id='$user_id'")->save($map);
        }
        header("Content-type:text/html;charset=utf-8");
        echo 'User表数据跑完!!';
        //$this->success('User表数据跑完!!','Index/index');
    }
	
	
    public function getqire(){
		$class_model = M("NovelClass");
		$class_list = $class_model->where('is_other=0')->select();
		$other_list = $class_model->where('is_other=1')->select();
		$this->assign('class_list', $class_list);
		$this->assign('other_list', $other_list);
		$this->display();
	}


	public function getqire1(){
		set_time_limit(0);
		$kw = $_POST['keywords'];

		//判断小说名是否存在
		$count = D('novel')->where("name='$kw'")->count();
		if($count > 0){
            $this->error('您搜索的小说已存在');
        }
		//获取分类内容
		$class_id = $_POST['class_id'];
		$other_class_id = $_POST['other_class_id'];
		if (empty($class_id) || empty($other_class_id)) {
			$this->error('请选择小说分类');
		}
		$class_model = M("NovelClass");
		$class_name_arr = $class_model->where("class_id=$class_id")->find();
		$other_class_arr = $class_model->where("class_id=$other_class_id")->find();

		//抓取部分
		$header = fakeIp();
		$cookie = "PHPSESSID=aluor37qhpojsknqjamvv1a111; qrxs_auth=db7absQKcVQpOGmMCAQ7kRLsS6yUdX3O4w26qlnLrvryjk9cV00TBNj4ezq%2BefzBr0OHAtlzVtThSNzb1xtbuXFR; default_channel=329694; local_user=1506667802440; noshowwxlay=yes; user_sex=1; Hm_lvt_fcd37298453821c3bc8b266221db88ac=1506667806; Hm_lpvt_fcd37298453821c3bc8b266221db88ac=1506670625; SERVERID=dfcbd24bb421be647196087b89b6f605|1506670621|1506667746;";

		//搜索ajax
		$data['key'] = $kw;
		$data['p'] = 1;
		$ajax_url = 'http://m.qirexiaoshuo.com/Book/ajax_search';
		$ajax_c = curl_request($ajax_url,$data);
		$search_content= json_decode($ajax_c,true);
		$sc = $search_content['data'];
		//小说名
		$name_start = strpos($sc,'<span class="orange">')+strlen('<span class="orange">');
		$name_end = strpos($sc,'</span>',$name_start);
		$name = substr($sc,$name_start,$name_end-$name_start);
		//echo $name;
		//图片
		$img_start = strpos($sc,'<img src="')+strlen('<img src="');
		$img_end = strpos($sc,'" class="avatar">',$img_start);
		$img = substr($sc,$img_start,$img_end-$img_start);
		$img_urls['url'] = $img;
		//使用文件服务器抓取图片
		$host = C('FILE_HOST_PATH');
		$curl_url = "$host/Home/File/uploadImg";
		$path = curl_request($curl_url,$img_urls);
		$path1 = json_decode($path,true);
		//小说id
		$bid_start = strpos($sc,'<a href="/book/')+strlen('<a href="/book/');
		$bid_end = strpos($sc,'/">',$bid_start);
		$bid = substr($sc,$bid_start,$bid_end-$bid_start);
		//作者名
		$author_start = strpos($sc,'<span class="author">')+strlen('<span class="author">');
		$author_end = strpos($sc,'</span>',$author_start);
		$author = substr($sc,$author_start,$author_end-$author_start);
		//介绍
		$description_start = strpos($sc,'</span><p>')+strlen('</span><p>');
		$description_end = strpos($sc,'</p>',$description_start);
		$description = substr($sc,$description_start,$description_end-$description_start);
		//完结
		$fu = 'http://w.qirexiaoshuo.com/book/'.$bid.'';
		$end_c = getUrlContent($fu,$header,$cookie,1);
		$end_start = strpos($end_c,'<i class="green">')+strlen('<i class="green">');
		$end_end = strpos($end_c,'</i>',$end_start);
		$end = substr($end_c,$end_start,$end_end-$end_start);
		if ($end == '连载中') {
			$is_end = 0;
		} else {
			$is_end = 1;
		}
		//novel数据入库
		$novel['name'] = $name;
		$novel['class_id'] = $class_id;
		$novel['class_name'] = $class_name_arr['class_name'];
		$novel['other_class_name'] = $other_class_arr['class_name'];
		$novel['other_class_id'] = $other_class_id;
		$novel['description'] = $description;
		$novel['img'] = $path1['path'];
		$novel['mini_img'] = empty($path1['path']) ? '' : $path1['path'];
		$novel['publish_time'] = date('Y-m-d H:i:s',time());
		$novel['word_count'] = '100万';
		$novel['is_end'] = $is_end;
		$novel['from'] = '奇热小说';
		$novel['writer'] = $author;
		$novel['web_from'] = $fu;
		$novel['is_shelf'] = 0;
		$book_id = D('novel')->add($novel);
		if($book_id){
			$url = "http://m.qirexiaoshuo.com/BookInfo/ajax_chapList?bid=$bid&sort=1&p=1&cid=0";
			$content = json_decode(curl_request($url));
			$count = $content->data->count;
			$url_str = $content->data->html;
			$total_page = $content->data->to_page;
			//目录for循环
			for($k=1;$k<=$total_page;$k++){
				//目录
				$url = "http://m.qirexiaoshuo.com/BookInfo/ajax_chapList?bid=$bid&sort=1&p=$k&cid=0";
				$content = json_decode(curl_request($url));
				$url_str = $content->data->html;
				$arr = explode('<li>',$url_str);
				$muli_arr = array_filter($arr);
				//内容入库准备
				$data = array();
				$index = 0;
				foreach($muli_arr as $key=>$aa){
					$ch_start = strpos($aa,'<a href="')+strlen('<a href="');
					$ch_end = strpos($aa,'"   >',$ch_start);
					$ch = substr($aa,$ch_start,$ch_end-$ch_start);
					$link = 'http://w.qirexiaoshuo.com'.$ch.'';
					//dump($link);
					$content = getUrlContent($link,$header,$cookie,1);
					//处理数据
					$chapter_start = strpos($content,'<header class="header clearfix"><h1 class="title">')+strlen('<header class="header clearfix"><h1 class="title">');
					$chapter_end = strpos($content,'</h1><div',$chapter_start);
					$chapter_lr_str = substr($content,$chapter_start,$chapter_end-$chapter_start);
					//获取章节
					$chapter_end_1 = strpos($chapter_lr_str,'章')+strlen('章');
					$chapter = substr($chapter_lr_str,0,$chapter_end_1);
					$chapter = trim($chapter);
					//获取标题
					$title = substr($chapter_lr_str,$chapter_end_1);
					$title = trim($title);
					//内容1
					// $content_start = strpos($content,'<div class="entry" id="read_conent_box">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;')+strlen('<div class="entry" id="read_conent_box">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
					// $content_end = strpos($content,'</div>',$content_start);
					// $content1 = substr($content,$content_start,$content_end-$content_start);
					// $chapter_content1 = "<p>" . trim($content1) . "</p>";
					// $order = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
					// $chpter_content = str_replace($order, '</p><p>', $chapter_content1);
					//内容2
					$content_start = strpos($content,'<div class="entry" id="read_conent_box">')+strlen('<div class="entry" id="read_conent_box">');
   				    $content_end = strpos($content,'</div>',$content_start);
					$chpter_content = substr($content,$content_start,$content_end-$content_start);
					//入novel_chapter数据库
					//获取chapter名字
					$or = 	30*($k-1)+$index+1;
					$tmp_arr['chapter'] = '第'.$or.'章';
					$tmp_arr['title'] = $title;
					$tmp_arr['content'] = $chpter_content;
					$tmp_arr['novel_name'] =$name;
					$tmp_arr['novel_id'] = $book_id;
					$tmp_arr['novel_mini_img'] = $path1['path'];

					$tmp_arr['order_set'] = $or;
					$tmp_arr['add_time'] = date('Y-m-d H:i:s');
					$word_count = $this->runzishu($tmp_arr['content']);
					$tmp_arr['word_count'] =$word_count;
					$data[] = $tmp_arr;
					if($word_count <= 200){
						echo '没钱了，请充值';
						die;
					}
	                //自动增加索引
					$index++;
				}
				if ($index >=30) {
					D('novel_chapter_add')->addAll($data);
					//清空数据
					$data = array();
					$index = 1;
				}
			}
			//循环外面
			if (!empty($data)) {
				D("novel_chapter_add")->addAll($data);
			}
			//将所有数据插入主表中
			$sql = "insert into `qw_novel_chapter`(`chapter`,`title`,`content`,`novel_name`,`novel_id`,`novel_mini_img`,`order_set`,`add_time`,`word_count`) select `chapter`,`title`,`content`,`novel_name`,`novel_id`,`novel_mini_img`,`order_set`,`add_time`,`word_count` from qw_novel_chapter_add where  `novel_id`='$book_id';";
			D()->execute($sql);
			//删除从表的数据
			D('novel_chapter_add')->where('novel_id='.$book_id)->delete();
			echo '恭喜您,抓取完毕';
		}

	}



	//奇热小说
	public function updatenovel7(){
		set_time_limit(0);
		//获取全本小说未完结的小说的信息
		$where['is_end'] = 0;
		$where['from'] = '奇热小说';
		$novel_start = $_GET['novel_start'] ? $_GET['novel_start'] : 0;
		$no_end_novel = D('novel')->where($where)->limit($novel_start, 1)->select();
		if (empty($no_end_novel)) {
			addlog("已更新了奇热小说");
			die('the end');
		}
		foreach($no_end_novel as $key=>$item){
			$novel_id = $item['id'];
			$map['novel_id'] = $item['id'];
			$web_from = $item['web_from'];
			$name = $item['name'];
			$mini_img = $item["mini_img"];
			preg_match_all('/[1234567890]/', $web_from, $id);
			$bid = implode('', $id[0]);
			//根据小说去查找章节表的最后一章
			$last_chapter = D('novel_chapter')->where($map)->order('id desc')->limit(1)->find();
			if (empty($last_chapter)) {
				echo '小说为空';
				continue;
			}
			$last_order_set = $last_chapter['order_set'];
			$last_title = $last_chapter['title'];
			//抓取部分
			$header = fakeIp();
			$cookie = "PHPSESSID=aluor37qhpojsknqjamvv1a111; qrxs_auth=db7absQKcVQpOGmMCAQ7kRLsS6yUdX3O4w26qlnLrvryjk9cV00TBNj4ezq%2BefzBr0OHAtlzVtThSNzb1xtbuXFR; default_channel=329694; local_user=1506667802440; noshowwxlay=yes; user_sex=1; Hm_lvt_fcd37298453821c3bc8b266221db88ac=1506667806; Hm_lpvt_fcd37298453821c3bc8b266221db88ac=1506670625; SERVERID=dfcbd24bb421be647196087b89b6f605|1506670621|1506667746;";
			$page = intval($last_order_set / 30) + 1;
			$url = "http://m.qirexiaoshuo.com/BookInfo/ajax_chapList?bid=$bid&sort=1&p=1&cid=0";
			$content = json_decode(curl_request($url));
			$total_page = $content->data->to_page;
			$count = $content->data->count;

			if($count == $last_order_set){
				echo '已经为最新章节';
			}
			//计算需要跟新的链接
			for ($i = $page; $i <= $total_page; $i++) {
				//分页链接
				$chapter_page_url[$i] = "http://m.qirexiaoshuo.com/BookInfo/ajax_chapList?bid=$bid&sort=1&p=$i&cid=0";
			}
			$first = reset($chapter_page_url);
			$first_key = key($chapter_page_url);
			//分页循环
			foreach($chapter_page_url as $k=>$v){
				$content = json_decode(curl_request($v));
				//dump($content);
				$url_str = $content->data->html;
				$count = $content->data->count;
				$arr = explode('<li>',$url_str);
				$muli_arr = array_filter($arr);
				//dump($count);die;
				$curl_k = $last_order_set%30+1;
				if($first_key == $k){
					$muli_arr = array_slice($muli_arr,$curl_k-1,30);
				}
				$ii = 1;
				//目录循环
				foreach($muli_arr as $vv){
					$ch_start = strpos($vv,'<a href="')+strlen('<a href="');
					$ch_end = strpos($vv,'"   >',$ch_start);
					$ch = substr($vv,$ch_start,$ch_end-$ch_start);
					$link = 'http://w.qirexiaoshuo.com'.$ch.'';
					//dump($link);die;
					$content = getUrlContent($link,$header,$cookie,1);
					//处理数据
					$chapter_start = strpos($content,'<header class="header clearfix"><h1 class="title">')+strlen('<header class="header clearfix"><h1 class="title">');
					$chapter_end = strpos($content,'</h1><div',$chapter_start);
					$chapter_lr_str = substr($content,$chapter_start,$chapter_end-$chapter_start);
					//获取章节
					$chapter_end_1 = strpos($chapter_lr_str,'章')+strlen('章');
					$chapter = substr($chapter_lr_str,0,$chapter_end_1);
					$chapter = trim($chapter);
					//获取标题
					$title = substr($chapter_lr_str,$chapter_end_1);
					$title = trim($title);
					//内容1
					// $content_start = strpos($content,'<div class="entry" id="read_conent_box">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;')+strlen('<div class="entry" id="read_conent_box">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
					// $content_end = strpos($content,'</div>',$content_start);
					// $content1 = substr($content,$content_start,$content_end-$content_start);
					// $chapter_content1 = "<p>" . trim($content1) . "</p>";
					// $order = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
					// $chpter_content = str_replace($order, '</p><p>', $chapter_content1);
					//内容2
					$content_start = strpos($content,'<div class="entry" id="read_conent_box">')+strlen('<div class="entry" id="read_conent_box">');
   				    $content_end = strpos($content,'</div>',$content_start);
					$chpter_content = substr($content,$content_start,$content_end-$content_start);
					//dump($chpter_content);die;
					//入novel_chapter数据库
					$or = $last_order_set + $ii;
					//获取chapter名字
					$tmp_arr['chapter'] = '第'.$or.'章';
					$tmp_arr['title'] = $title;
					$tmp_arr['content'] = $chpter_content;
					$tmp_arr['novel_name'] =$name;
					$tmp_arr['novel_id'] = $novel_id;
					$tmp_arr['novel_mini_img'] = $mini_img;
					$tmp_arr['order_set'] = $or;
					$tmp_arr['add_time'] = date('Y-m-d H:i:s');
					$word_count = $this->runzishu($tmp_arr['content']);
					$tmp_arr['word_count'] =$word_count;
					if($word_count <= 200){
						echo '没钱了，请充值';
						die;
					}
					$ii++;
					sleep(1);
					D('novel_chapter')->add($tmp_arr);
				}
			}
		}
		$domain = $_SERVER["HTTP_HOST"];
		$this->success('抓取下篇小说', 'http://' . $domain . '/Qwadmin/novel/updatenovel7?novel_start=' . ($novel_start + 1), 1);

	}
	
	
	
	
	
	/**
	 * 歪歪小说部分
	 */
	public function getwaiwai(){
		$class_model = M("NovelClass");
		$class_list = $class_model->where('is_other=0')->select();
		$other_list = $class_model->where('is_other=1')->select();
		$this->assign('class_list', $class_list);
		$this->assign('other_list', $other_list);
		$this->display();
	}



	public function getwaiwai1(){
		set_time_limit(0);
		$keyword = $_POST['keywords'];

		//判断小说名是否存在
		$count = D('novel')->where("name='$keyword'")->count();
		if($count > 0){
			$this->error('您搜索的小说已存在');
		}
		//获取分类内容
		$class_id = $_POST['class_id'];
		$other_class_id = $_POST['other_class_id'];
		if (empty($class_id) || empty($other_class_id)) {
			$this->error('请选择小说分类');
		}
		$class_model = M("NovelClass");
		$class_name_arr = $class_model->where("class_id=$class_id")->find();
		$other_class_arr = $class_model->where("class_id=$other_class_id")->find();

		//抓取部分
		$header = fakeIp();
		$cookie = "PHPSESSID=8668d9bf60493eb91e44fdeb36e688ba; uid=722370; openid=o_KT2vlm2ZZil27aqlctjGOkwxEs; uname=%E6%B2%A1%E6%9C%89%E5%90%8D%E5%AD%97; checkstr=629a4077c3fe5f3c2c3ac1bfaefe1dc5; sid=8668d9bf60493eb91e44fdeb36e688ba; UM_distinctid=15f04c3ed0c10-0bb69da1e-3a453759-1fa400-15f04c3ed0d353; CNZZDATA1261944900=1010281732-1507608346-%7C1507612760; channel_id=69092809; promotions_id=1";
		$kw = urlencode($keyword);
		$search_url = 'https://wap.83xiaoshuo.cn/book/searchresult?kw='.$kw;
		$searchC = getUrlContent($search_url,$header,$cookie,1);
		//获取书本id
		$book_id_start = strpos($searchC,'<div class="imgcontent  clearfix">
  <a href="')+strlen('<div class="imgcontent  clearfix">
  <a href="');
		$book_id_end = strpos($searchC,'">',$book_id_start);
		$book_id = substr($searchC,$book_id_start,$book_id_end-$book_id_start);
		//小说详情页面
		$xiangq_url = 'https://wap.83xiaoshuo.cn'.$book_id;
		$xiangqC = getUrlContent($xiangq_url,$header,$cookie,1);
		 //描述
		$description_start = strpos($xiangqC,' <div class="jianjie_content">')+strlen(' <div class="jianjie_content">');
		$description_end = strpos($xiangqC,'</div>',$description_start);
		$description = substr($xiangqC,$description_start,$description_end-$description_start);
		//图片
		$img_start = strpos($xiangqC,'<img data-original="')+strlen('<img data-original="');
		$img_end = strpos($xiangqC,'">',$img_start);
		$img = substr($xiangqC,$img_start,$img_end-$img_start);
		$img_urls['url'] = $img;
		//使用文件服务器抓取图片
		$host = C('FILE_HOST_PATH');
		$curl_url = "$host/Home/File/uploadImg";
		$path = curl_request($curl_url,$img_urls);
		$path1 = json_decode($path,true);
		//字数
		$word_count_start = strpos($xiangqC,'</a></div>
          <div>字数：')+strlen('</a></div>
          <div>字数：');
		$word_count_end = strpos($xiangqC,'</div>',$word_count_start);
		$word_count = substr($xiangqC,$word_count_start,$word_count_end-$word_count_start);
		//作者
		$author_start = strpos($xiangqC,'<div class="zbtj_details">
          <div>作者：')+strlen('<div class="zbtj_details">
          <div>作者：');
		$author_end = strpos($xiangqC,'</div>',$author_start);
		$author = substr($xiangqC,$author_start,$author_end-$author_start);
		//小说名称
		$name_start = strpos($xiangqC,' <div class="zbtj_title ">')+strlen(' <div class="zbtj_title ">');
		$name_end = strpos($xiangqC,'</div>',$name_start);
		$name = substr($xiangqC,$name_start,$name_end-$name_start);
		//是否完结
		$is_end = 1;
		if (strpos($xiangqC,'<div class="imgfl label lianzai">')) {
			$is_end = 0;
		}
		//小说id
		preg_match_all('/[1234567890]/', $book_id, $bi);
		$bookid = implode('',$bi[0]);
		//小说总章节
		$total_start = strpos($xiangqC,'<span class="pl_count">')+strlen('<span class="pl_count">');
		$total_end = strpos($xiangqC,'</span></div>',$total_start);
		$total = substr($xiangqC,$total_start,$total_end-$total_start);
		preg_match_all('/[1234567890]/', $total, $totaos);
		$Tpage = implode('',$totaos[0]);
		$total_page = ceil($Tpage/30);
		//novel数据入库
		$novel['name'] = $name;
		$novel['class_id'] = $class_id;
		$novel['class_name'] = $class_name_arr['class_name'];
		$novel['other_class_name'] = $other_class_arr['class_name'];
		$novel['other_class_id'] = $other_class_id;
		$novel['description'] = $description;
		$novel['img'] = $path1['path'];
		$novel['mini_img'] = empty($path1['path']) ? '' : $path1['path'];
		$novel['publish_time'] = date('Y-m-d H:i:s',time());
		$novel['word_count'] = $word_count;
		$novel['is_end'] = 0;
		$novel['from'] = '歪歪书城';
		$novel['write'] = $author;
		$novel['web_from'] = $xiangq_url;
		$novel['is_shelf'] = $is_end;
		$id = D('novel')->add($novel);
		if($id){
			//目录抓取
			for($i =1;$i<= $total_page;$i++){
				//目录抓取
				$muli_url = "https://wap.83xiaoshuo.cn/book/chapter?bookid=2024076&bookid=$bookid&page=$i";
				$muliC = getUrlContent($muli_url,$header,$cookie,1);
				if($i > 1){
					$muliC = json_decode($muliC,true);
					$list = $muliC['result']['data']['html'];
				}else{
					$list_start = strpos($muliC, '<ul class="list5 ">') + strlen('<ul class="list5 ">');
					$list_end = strpos($muliC, '</ul>', $list_start);
					$list = substr($muliC, $list_start, $list_end - $list_start);
				}
				//目录转化为数组
				$listarray = explode('<li class="clearfix">',$list);
				array_shift($listarray);
				//dump($listarray);die;
				$index = 0;
				$data =array();
				//每页遍历获取小说内容
				foreach($listarray as $key=>$v){
					//章节链接
					$cp_start = strpos($v, '<a href="') + strlen('<a href="');
					$cp_end = strpos($v, '">', $cp_start);
					$cpu = substr($v, $cp_start, $cp_end - $cp_start);
					$cpu= str_replace('&amp;','&',$cpu);
					//收费判断
					$pay = strpos($cpu,'website_token');
					if($i > 1){
						$Curl = $cpu;
					}else{
						$Curl = "https://wap.83xiaoshuo.cn".$cpu;
						if($pay != 0){
							$Curl = $cpu;
						}
					}
					$chapter_con = getUrlContent($Curl,$header,$cookie,1);
					//获取章节
					$chapter_start = strpos($v,'<div class="fl">')+strlen('<div class="fl">');
					$chapter_end = strpos($v,'</div>',$chapter_start);
					$chapterC = substr($v, $chapter_start, $chapter_end - $chapter_start);
					$chapterArr = explode(' ',$chapterC);
					$chapter = $chapterArr[0];
					//获取标题
					$title = $chapterArr[1];
					//获取章节的内容
					$content_start = strpos($chapter_con, '<div class="articleContent nodis">') + strlen('<div class="articleContent nodis">');
					$content_end = strpos($chapter_con, '</div>', $content_start);
					$content_str = substr($chapter_con, $content_start, $content_end - $content_start);
					$content_str = str_replace('<br><br>', '</p><p>', $content_str);
					$content_str = str_replace('<br/><br/>', '</p><p>', $content_str);
					$content_str = str_replace('</br></br>', '</p>', $content_str);
					if(empty($content_str)){
						echo '请充值';die;
					}
					//入chapter表
					preg_match('/\d+/',$chapter,$match);
					$order_set = $match[0];
					$tmp_arr['chapter'] = $chapter;
					$tmp_arr['title'] = empty($title) ? '' : $title;
					$tmp_arr['content'] = $content_str;
					$tmp_arr['word_count'] = $this->runzishu($tmp_arr['content']);
					$tmp_arr['novel_name'] = $name;
					$tmp_arr['novel_id'] = $id;
					$tmp_arr['novel_mini_img'] = empty($path1['path']) ? '' : $path1['path'];
					$tmp_arr['order_set'] = intval($order_set);
					$tmp_arr['add_time'] = date('Y-m-d H:i:s');
					//将临时数组赋值给$data
					$data[] = $tmp_arr;
					$index++;
				}
				if ($index >=30) {
					D('novel_chapter_add')->addAll($data);
					//清空数据
					$data = array();
					$index = 1;
				}
			}
			if(!empty($data)){
				D('novel_chapter_add')->addAll($data);
			}
			//将所有数据插入主表中
			$sql = "insert into `qw_novel_chapter`(`chapter`,`title`,`content`,`novel_name`,`novel_id`,`novel_mini_img`,`order_set`,`add_time`,`word_count`) select `chapter`,`title`,`content`,`novel_name`,`novel_id`,`novel_mini_img`,`order_set`,`add_time`,`word_count` from qw_novel_chapter_add where  `novel_id`='$id';";
			D()->execute($sql);
			//删除从表的数据
			D('novel_chapter_add')->where('novel_id='.$id)->delete();
			echo '恭喜您,抓取完毕';
		}

	}

    /**
     * 歪歪小说更新
     */
    public function updatewaiwai(){
        set_time_limit(0);
        $start_id = $_GET["start_id"] ? $_GET["start_id"] : 0;
        $nmap["is_end"] = 0;
        $nmap["from"] = "歪歪书城";
        $update_novel_list = D("novel")->where($nmap)->order("id asc")->limit("$start_id, 1")->select();
        //print_r($update_novel_list);die;
        //COOKIE值
        $cookie = "PHPSESSID=8668d9bf60493eb91e44fdeb36e688ba; uid=722370; openid=o_KT2vlm2ZZil27aqlctjGOkwxEs; uname=%E6%B2%A1%E6%9C%89%E5%90%8D%E5%AD%97; checkstr=629a4077c3fe5f3c2c3ac1bfaefe1dc5; sid=8668d9bf60493eb91e44fdeb36e688ba; UM_distinctid=15f04c3ed0c10-0bb69da1e-3a453759-1fa400-15f04c3ed0d353; CNZZDATA1261944900=1010281732-1507608346-%7C1507612760; channel_id=69092809; promotions_id=1";

        if(empty($update_novel_list)){
            addlog("更新了最爽小说");
            die("the end");
        }

        foreach($update_novel_list as $key=>$item) {
            $domain_waiwai = 'https://wap.83xiaoshuo.cn';
            $novel_id = $update_novel_list[0]["id"] ? $update_novel_list[0]["id"] : 3;
            //echo $novel_id;die;

            //获得小说的信息
            $novel_info = D("novel")->where("id = $novel_id")->find();

            //获得小说的最新章节
            $last_chapter = D("novel_chapter")->where("novel_id = $novel_id")->order("order_set desc")->find();
            //echo D("novel_chapter")->getLastSql();

            //echo $last_chapter["chapter"]." ".$novel_info["web_from"]."<br>";die;
            //模拟微信浏览器访问页面
            $header = fakeIp();
            //$cookie = "PHPSESSID=b131b3e837f61475bf37f85641e0ac6d; uid=21433576; openid=ow7-lwD5Stboe4pp2nW4DD4A3cyw; uname=%E6%AC%A7%E4%BA%9A%E4%BA%91%E4%BA%A4%E6%98%93%E5%AE%A2%E6%9C%8D-%E5%B0%8F%E4%B9%90; checkstr=e1745060a715236a1bb7f6fae365ab52; sid=b131b3e837f61475bf37f85641e0ac6d; sex=2; UM_distinctid=15c497afcb780-0df9e9464-6a320e21-1fa400-15c497afcb93a3; CNZZDATA1261580920=686923403-1495880007-https%253A%252F%252Fopen.weixin.qq.com%252F%7C1495878368; channel_id=1; promotions_id=1";
            $content = getUrlContent($novel_info["web_from"], $header, $cookie, 1);

            //echo $content;die;
            //dump($content);die;
            //查看全部目录
            $all_chapter_start = strpos($content, '<li class="more bnone" >') + strlen('<li class="more bnone" >');
            $all_chapter_start = strpos($content, '<a href="', $all_chapter_start) + strlen('<a href="');
            $all_chapter_end = strpos($content, '"', $all_chapter_start);
            $all_chapter_str = substr($content, $all_chapter_start, $all_chapter_end - $all_chapter_start);
            //$new_str = substr($new_str, 0, strpos($new_str, " "));
            //echo $new_str;die;
            $all_chapter_url = $domain_waiwai.$all_chapter_str;
            //echo $all_chapter_url;die;

            $all_chapter_content = getUrlContent($all_chapter_url,$header, $cookie, 1);
            //echo $all_chapter_content;die;
            //dump($all_chapter_content);die;

            //已更新至多少章
            $new_start = strpos($all_chapter_content, 'class="mulutitle">') + strlen('class="mulutitle">');
            $new_end = strpos($all_chapter_content, '<a', $new_start);
            $new_str = substr($all_chapter_content, $new_start, $new_end - $new_start);
            preg_match('/\d+/',$new_str,$new_chapter);
            $new_order_set = $new_chapter[0];
            //dump($new_chapter);die;
            //判断最新章节是否和我带数据相同
            if($new_order_set <= $last_chapter["order_set"]){
                echo "《".$item["name"]."》"."没有更新章节";
                continue;
            }else{ //不相同我们就更新他
                //获取最后一章节
                //计算它是第几页
                $order_set = $last_chapter["order_set"];
                $page = intval($order_set / 30) + 1;
                //echo $page;die;

                //获得第一个目录的链接地址
                $menu_start = strpos($content, '<div class="pub-btn mt-10 pb-0">') + strlen('<div class="pub-btn mt-10 pb-0">');
                $menu_start = strpos($content, '<a href="', $menu_start) + strlen('<a href="');
                $menu_end = strpos($content, '"', $menu_start);
                $menu_url = substr($content, $menu_start, $menu_end - $menu_start);
                $url_list[$page] = $domain_waiwai.$menu_url."&order_by=order%20asc&page=".$page;
                //获得最后一页
                $last_page = intval($new_order_set / 30) + 1;

                for($i = $page; $i <= $last_page; $i++){
                    $url_list[$i] = $all_chapter_url."&order_by=order%20asc&page=".$i;
                }
                foreach($url_list as $ukey=>$uitem) {
                    //echo $menu_start." ".$menu_end."<br>";
                    //echo $menu_url;
                    //直接打开列表页面，同时跳转到我们数据对应的那一页。
                    $header = fakeIp();

                    $cha_list = getUrlContent($uitem, $header, $cookie, 1);
                    $cha_list = json_decode($cha_list,true)['result']['data']['html'];
                    $list = explode('<a href="', $cha_list);
                    array_shift($list);
                    //print_r($list);die;
                    $index = 0;
                    $data = array();
                    foreach($list as $key=>$item){
                        //echo $item."<br>";
                        //获得URL
                        $url = str_replace('&amp;','&',substr($item, 0, strpos($item, '"')));
                        //dump($item);die;
                        //获得章节
                        $chapter_start = strpos($item,'<div class="fl">')+strlen('<div class="fl">');
                        $chapter_end = strpos($item,'</div>',$chapter_start);
                        $chapter = substr($item,$chapter_start,$chapter_end-$chapter_start);
                        $title = explode(' ',$chapter)[1];
                        $chapter = mb_substr($chapter, 0,  (mb_strpos($chapter, "章", null, "UTF-8") + mb_strlen("章", "UTF-8")), "UTF-8");
                        //获得order_set
                        $item_order_set =  mb_substr($chapter, (mb_strpos($chapter, "第", null, "UTF-8") + mb_strlen("第", "UTF-8")), mb_strpos($chapter, "章", null, "UTF-8") - (mb_strpos($chapter, "第", null, "UTF-8") + mb_strlen("第", "UTF-8")), "UTF-8");
                        //dump($item_order_set);die;
                        if(strlen($url) > 0 && strlen($chapter) > 0) {
                            if($item_order_set > $order_set) {
                                //echo $chapter." ".$url."<br>";die;
                                $header = fakeIp();
                                $chapter_content = getUrlContent($url,$header,$cookie,1);
                                //echo $chapter_content;die;

                                //判断抓取是否有效
                                $is_error = strpos($chapter_content, "https://res.wx.qq.com/open/libs/weui/0.4.1/weui.css");
                                if(!empty($is_error)) {
                                    //print_r($rs);
                                    //echo $chapter_content;
                                    die("我呢个去你的cookie有问题,问题出现在".$novel_info["novel_name"]."下的第".$ukey."页的第".($key)."条");
                                }

                                //dump($chapter_content);die;

                                //判断是否已经没有钱了
                                //如果没有就不添加数据
// 								$no_money = strpos($chapter_content, "<title>最爽小说-充值</title>");
// 								if(!empty($no_money))
// 									die("兄弟你没钱了")
                                if(strlen($chapter_content) == 0)
                                    die("兄弟你没钱了");

                                //echo "1111".$chapter_content;die;

                                //获取标题 title
                                $title_start = strpos($chapter_content, '<div class="titlename fl">') + strlen('<div class="titlename fl">');
                                $title_start= strpos($chapter_content, '章', $title_start) + strlen('章');
                                $title_end = strpos($chapter_content, '</div>', $title_start);
                                $title = trim(substr($chapter_content, $title_start, $title_end- $title_start));
                                //dump($title);die;
                                //获取第几章节(有中文)chapeter
                                $cha_start = strpos($chapter_content, '<div class="titlename fl">') + strlen('<div class="titlename fl">');
                                $cha_start = strpos($chapter_content, '第', $cha_start);
                                $cha_end = strpos($chapter_content, '章', $cha_start) + strlen('章');
                                $chapter_value= substr($chapter_content, $cha_start, $cha_end- $cha_start);
                                //echo $title."<br>".$chapter_value;die;

                                //判断是否章节重复
                                $map["novel_id"] = $novel_info["id"];
                                $map["chapter"] = $chapter_value;
                                $count = D("novel_chapter")->where($map)->count();
                                //echo $count;die;
                                if($count > 0)
                                    continue;

                                //标题赋值
                                $data[$index]["title"] = $title ? $title : " ";
                                $data[$index]["chapter"] = $chapter_value;

                                //获取order_set
                                $order_start = strpos($data[i]["chapter"], '第') + strlen('第');
                                $order_end = strpos($data[i]["chapter"], '章', $order_start);
                                $data[$index]["order_set"] = substr($data[$index]["chapter"], $order_start, $order_end- $order_start);
                                //dump($chapter_content);die;
                                //获取章节内容
                                $c_pos_start = strpos($chapter_content, '<div class="articleContent nodis">') + strlen('<div class="articleContent nodis">');
                                $c_pos_end = strpos($chapter_content, '</div>', $c_pos_start);
                                $data[$index]["content"]= substr($chapter_content, $c_pos_start, $c_pos_end - $c_pos_start);
                                //dump($data);die;
                                //设置数据
                                $data[$index]["novel_name"] = $novel_info["name"];
                                $data[$index]["novel_id"] = $novel_info["id"];
                                $data[$index]["novel_mini_img"] = $novel_info["mini_img"];
                                $data[$index]["add_time"] = date("Y-m-d H:i:s");

                                //print_r($data);die;

                                //索引自动增长
                                $index++;
                            }
                        }
                    }
                    if(!empty($data))
                        $number = D("novel_chapter")->addAll($data);

                    //推送消息
                    /*if ($number > 0) {
                        //就证明更新了
                        //推送消息
                        $arrr = D('novel_chapter')->field('id,title,novel_id,novel_name,order_set,add_time')
                            ->order('id desc')->limit(1)->select();
                        //dump($arrr);die;
                        foreach ($arrr as $arr){
                            $push_chapter_id = $arr['id'];
                            $push_novel_id = $arr['novel_id'];
                            $push_novel_name = $arr['novel_name'];
                            $push_title = $arr['title'];
                            $push_order_set = $arr['order_set'];
                            $push_add_time = $arr['add_time'];
                            $this->novelpush($push_novel_id,$push_chapter_id,$push_novel_name,$push_order_set,$push_title,$push_add_time);
                        }
                    }*/

                }
            }
        }
        die('the end');

// 		if($start_id > 0) {
        //echo "the end";
        //die;
// 		}

        $next_start_id = $start_id + 1;
        //$this->redirect("/Qwadmin/Novel/updatenovel?start_id=$next_start_id");
        $this->success("跳转到下一章节","/Qwadmin/Novel/updatenovel?start_id=$next_start_id",1);
        //echo "the end";
        //echo $content;
    }

		/**
	 * 色小说
	 */
	public function getse(){
		$class_model = M("NovelClass");
		$class_list = $class_model->where('is_other=0')->select();
		$other_list = $class_model->where('is_other=1')->select();
		$this->assign('class_list', $class_list);
		$this->assign('other_list', $other_list);
		$this->display();
	}




	public function getse1(){
		set_time_limit(0);
		$keyword = $_POST['keywords'];

		//判断小说名是否存在
		$count = D('novel')->where("name='$keyword'")->count();
		if($count > 0){
			$this->error('您搜索的小说已存在');
		}
		//获取分类内容
		$class_id = $_POST['class_id'];
		$other_class_id = $_POST['other_class_id'];
		if (empty($class_id) || empty($other_class_id)) {
			$this->error('请选择小说分类');
		}
		$class_model = M("NovelClass");
		$class_name_arr = $class_model->where("class_id=$class_id")->find();
		$other_class_arr = $class_model->where("class_id=$other_class_id")->find();

		$search_url = 'https://www.yite.cc/search.php?q='.urlencode($keyword);
		$sc = getUrlContent($search_url,0,0,1);
		//找到具体书本
		$book_start = strpos($sc,'<dt><a href="')+strlen('<dt><a href="');
		$book_end = strpos($sc,'" title=',$book_start);
		$book = substr($sc,$book_start,$book_end-$book_start);
		//获取基本信息
		$xiangqC = getUrlContent($book,0,0,1);
		//描述
		$description_start = strpos($xiangqC,'<div id="des">')+strlen('<div id="des">');
		$description_end = strpos($xiangqC,'</div>',$description_start);
		$description = substr($xiangqC,$description_start,$description_end-$description_start);
		//图片
		$img_start = strpos($xiangqC,'<div id="bookinfo-left">
            <div id="bookimg"><img src="')+strlen('<div id="bookinfo-left">
            <div id="bookimg"><img src="');
		$img_end = strpos($xiangqC,'" width=',$img_start);
		$img = substr($xiangqC,$img_start,$img_end-$img_start);
		$img_urls['url'] = $img;
		//使用文件服务器抓取图片
		$host = C('FILE_HOST_PATH');
		$curl_url = "$host/Home/File/uploadImg";
		$path = curl_request($curl_url,$img_urls);
		$path1 = json_decode($path,true);
		//作者
		$author_start = strpos($xiangqC,'<div id="writer">作者：')+strlen('<div id="writer">作者：');
		$author_end = strpos($xiangqC,'</div>',$author_start);
		$author = substr($xiangqC,$author_start,$author_end-$author_start);

		//小说名称
		$name_start = strpos($xiangqC,'<div id="bookinfo-right">
            <h1>')+strlen('<div id="bookinfo-right">
            <h1>');
		$name_end = strpos($xiangqC,'</h1>',$name_start);
		$name = substr($xiangqC,$name_start,$name_end-$name_start);

		//是否完结
		$is_end = 1;
		if (strpos($xiangqC,'<span style="color:#fff" class="status_text">连载中</span>')) {
			$is_end = 0;
		}
		//novel数据入库
		$novel['name'] = $name;
		$novel['class_id'] = $class_id;
		$novel['class_name'] = $class_name_arr['class_name'];
		$novel['other_class_name'] = $other_class_arr['class_name'];
		$novel['other_class_id'] = $other_class_id;
		$novel['description'] = $description;
		$novel['img'] = $path1['path'];
		$novel['mini_img'] = empty($path1['path']) ? '' : $path1['path'];
		$novel['publish_time'] = date('Y-m-d H:i:s',time());
		$novel['word_count'] = '100万';
		$novel['is_end'] = 0;
		$novel['from'] = '色小说';
		$novel['writer'] = $author;
		$novel['web_from'] = $search_url;
		$novel['is_shelf'] = $is_end;
		$id = D('novel')->add($novel);
		if($id){
			//目录截取
			$mulu_start = strpos($xiangqC,'<div id="list">
            <ul>')+strlen('<div id="list">
            <ul>');
			$mulu_end = strpos($xiangqC,'</ul>
        </div>',$mulu_start);
			$mulu = substr($xiangqC,$mulu_start,$mulu_end-$mulu_start);
			//目录转化成数组
			$muluArray = explode('<li>',$mulu);
			array_shift($muluArray);
			$maxkey = array_search(max($muluArray),$muluArray);
			$total = $maxkey+1;
			foreach($muluArray as $key=>$value){
				//章节链接截取
				$link_start = strpos($value,'<a href="')+strlen('<a href="');
				$link_end = strpos($value,'">',$link_start);
				$link = substr($value,$link_start,$link_end-$link_start);
				$link = $book.$link;
				//获取章节
				$chapter_start = strpos($value,'">')+strlen('">');
				$chapter_end = strpos($value,'</a></li>',$chapter_start);
				$chapterC = substr($value, $chapter_start, $chapter_end - $chapter_start);
				//1 依空格分割
				$chapterArr = explode(' ',$chapterC);
				//2 依:分割
				//$chapterArr = explode('：',$chapterC);
				//章节
				$chapter = $chapterArr[0];
				//获取标题
				$title = $chapterArr[1];
				//章节内容页面
				$content = getUrlContent($link,0,0,1);
				//获取章节的内容
				$content_start = strpos($content, '<div id="content">') + strlen('<div id="content">');
				$content_end = strpos($content, '</div>', $content_start);
				$content_str = substr($content, $content_start, $content_end - $content_start);
				// if(empty($content_str)){
					// die('挂了');
				// }
				//入chapter表
				$order_set = $key+1;
				$tmp_arr['chapter'] = '第'.$order_set.'章';
				$tmp_arr['title'] = empty($title) ? '' : $title;
				$tmp_arr['content'] = $content_str;
				$tmp_arr['word_count'] = $this->runzishu($tmp_arr['content']);
				$tmp_arr['novel_name'] = $name;
				$tmp_arr['novel_id'] = $id;
				$tmp_arr['novel_mini_img'] = empty($path1['path']) ? '' : $path1['path'];
				$tmp_arr['order_set'] = intval($order_set);
				$tmp_arr['add_time'] = date('Y-m-d H:i:s');
				D('novel_chapter_add')->add($tmp_arr);
			}
		}
		//将所有数据插入主表中
		$sql = "insert into `qw_novel_chapter`(`chapter`,`title`,`content`,`novel_name`,`novel_id`,`novel_mini_img`,`order_set`,`add_time`,`word_count`) select `chapter`,`title`,`content`,`novel_name`,`novel_id`,`novel_mini_img`,`order_set`,`add_time`,`word_count` from qw_novel_chapter_add where  `novel_id`='$id';";
		D()->execute($sql);
		//删除从表的数据
		D('novel_chapter_add')->where('novel_id='.$id)->delete();
		echo '恭喜您,抓取完毕';
	}




	
	
	
}